kindly give an example, i am new to return number of ingredients. new to ruby and having prob with syntax.
class Recipe
include Enumerable
attr_reader :name
def initialize(name, cuisine, ingredients, steps)
#name = name
#cuisine = cuisine
#ingredients = ingredients
#steps = steps
end
def name
#name
end
def cuisine
#cuisine
end
def ingredients
#ingredients
end
def steps
#steps
end
def display
puts "Recepie Manager"
puts "\n Name:", #name, "\n Cuisine:", #cuisine ,"\n Ingredients and Quantity:", #ingredients, "Steps:", #steps
end
def how_many_ingredients
puts "Number of Ingridents:" , #ingredients
end
end
obj1 = Recipe.new('Briyani', 'Indian','chicken,rice,spices,blah,blah','cook and eat')
obj1.display
From the code it looks like you have misplaced your end statement. You have to create objects of class after it ends.
class Receipe
include Enumerable
attr_reader :name
def initialize(name, cuisine, ingredients, steps)
#name = name
#cuisine = cuisine
#ingredients = ingredients
#steps = steps
end
def name
#name
end
def cuisine
#cuisine
end
def ingredients
#ingredients
end
def steps
#steps
end
def display
puts "Recepie Manager"
puts "\n Name:", #name, "\n Cuisine:", #cuisine ,"\n Ingredients and Quantity:", #ingredients, "Steps:", #steps
end
def how_many_ingredients
puts "Number of Ingridents:" , #ingredients
end
end
obj1 = Receipe.new('Briyani', 'Indian','chicken,rice,spices,blah,blah','cook and eat')
obj1.display
Thanks,
I used my guru "Google" for finding solution. For returning the ingredients count i used split and count methods.
def how_many_ingredients
ingredients = #ingredients.split(",")
puts "Number of Ingridents:" , ingredients.count
end
you can use gets.chomp to get input from users if thats what you mean
class Receipe
include Enumerable
attr_reader :name
def initialize(name, cuisine, ingredients, steps)
#name = name
#cuisine = cuisine
#ingredients = ingredients
#steps = steps
end
def name
#name
end
def cuisine
#cuisine
end
def ingredients
#ingredients
end
def steps
#steps
end
def display
puts "Recepie Manager"
puts "\n Name:", #name, "\n Cuisine:", #cuisine ,"\n Ingredients and Quantity:", #ingredients, "Steps:", #steps
end
def how_many_ingredients
puts "Number of Ingridents:" , #ingredients
end
end
yourname = gets.chomp
yourcuisine = gets.chomp
youringridents = gets.chomp
yoursteps = gets.chomp
obj1 = Receipe.new(yourname, yourcuisine,youringridents,yoursteps)
obj1.display
Related
I can't come up with a solution.
class Person
def initialize(name)
#name = name
end
def greet(other_name)
#other_name
print "Hi #{#other_name}, my name is #{#name}"
end
end
kit = Person.new("Tom", "Jerry")
kit.greet
I would appreciate a helping hand.
You have to make a decision:
Do you want to provide both names when initializing the Person:
class Person
def initialize(name, other)
#name = name
#other = other
end
def greet
puts "Hi #{#other}, my name is #{#name}"
end
end
kit = Person.new("Tom", "Jerry")
kit.greet
#=> Hi Jerry, my name is Tom
Or do you want to provide the second name when calling the greet method:
class Person
def initialize(name)
#name = name
end
def greet(other)
puts "Hi #{other}, my name is #{#name}"
end
end
kit = Person.new("Tom")
kit.greet("Jerry")
#=> Hi Jerry, my name is Tom
In the initialize method, you should take in two parameters, because you are calling it with two. You were declaring the #other_name variable inside the greet function instead of the initialize one.
This will work.
class Person
def initialize(name, other_name)
#name = name
#other_name = other_name
end
def greet
print "Hi #{#other_name}, my name is #{#name}"
end
end
kit = Person.new("Tom", "Jerry")
kit.greet
https://repl.it/C5wn
Consider writing your code like this:
class Person
attr_reader :name
def initialize(name)
#name = name
end
def greet(person)
puts "Hi #{person.name}, my name is #{name}"
end
end
tom = Person.new("Tom")
jer = Person.new("Jerry")
tom.greet(jer) #=> Hi Jerry, my name is Tom.
This way you actually have another person as an object instead of just a name.
I have this class:
class Player
attr_accessor :card_pile, :name
def initialize
#name = name
#bust = false
#card_pile = []
end
def bust?
return #cards.inject(:+) > 21
end
end
I also have this as the beginning of another class
def playing_game
puts "How many players are playing? "
players_amount = gets.chomp.to_i
(0...players_amount).each do
puts ("What is the players name? ")
#name = gets.chomp
#players.push(#name)
end
#players.each do |each_player|
#name = Player.new
while true
while #name.card_pile.length < 2 do
new_card = Card.new
#name.card_pile.push(new_card.value)
end
puts(#name.card_pile)
print #name, "'s turn" "\n"
At the moment this will print out #<Player:0x007fc14984a4b0>'s turn instead of Rich's turn
Why is this happening? I thought I had made an instance variable in the Player class and then instantiated this class #name = Player.new and then could reference it from here on out??
This will help
def playing_game
puts 'How many players are playing?'
players_amount = gets.chomp.to_i
players_names = (0...players_amount).map do
puts ("What is the players name? ")
gets.chomp
end
players_names.each do |player_name|
player = Player.new(player_name)
while player.card_pile.length < 2 do
new_card = Card.new
player.card_pile.push(new_card.value)
end
puts player.card_pile
puts "#{player.name}'s turn"
end
end
UPD:
You don't need instance variables (like #name and #players inside single method).
In this code you iterate over players names
#players.each do |each_player|
=>
players_names.each do |player_name|
In context of
#name = Player.new
name is a Player instance
to create player with given name pass it to initializer:
player = Player.new(player_name)
then call name on Player instance, that you create earlier
puts "#{player.name}'s turn"
I have this classes (just a simplified example, not real ones)
class Shop
attr_accessor :name, :products
def initialize(name, products_names=%w(apple orange apple cucumber fennel))
self.name = name
self.products = products_names.map {|name| Product.new(self, name)}
end
end
class Product
attr_accessor :shop, :name
def initialize(shop, name)
self.shop = shop
self.name = name
end
def existing_products_count # problem_comes_here
shop.products #i need to process all products initialized for current shop
.select{|p| p.name==name}.size
end
def uniq_code
"#{name}_#{existing_products_count+1}"
end
end
And here is two questions:
Is this a good approach to pass self for Product instance initialization
and
How can i solve my case to process all already existing shop products for new product initialization
Thank you
UPDATE
all i invented for now is (at least it works like i need)
class Shop
attr_accessor :name, :products
def initialize(name, products_names=%w(apple orange apple cucumber fennel))
Product.class_variable_set(:##all, []) # << added
self.name = name
self.products = products_names.map {|name| Product.new(self, name)}
end
end
class Product
attr_accessor :shop, :name
def self.all # << added
##all
end
def initialize(shop, name)
self.shop = shop
self.name = name
self.class.all << self # << added
end
def existing_products_count # problem goes away here
self.class.all.products # << changed
.select{|p| p.name==name}.size
end
def uniq_code
"#{name}_#{existing_products_count+1}"
end
end
but i feel bad about this kind of solution (i don't know why) and will be appreciate for better one
I'm not sure if I understand the question, but here is one interpretation. Note how I've changed the method Product#uniq_code.
class Shop
attr_accessor :name, :products
def initialize(name, products_names=%w(apple orange apple cucumber fennel))
self.name = name
self.products = products_names.map {|name| Product.new(self, name)}
end
end
class Product
attr_accessor :shop, :name
def initialize(shop, name)
self.shop = shop
self.name = name
end
def uniq_code # problem_comes_here
puts "buy #{self.name} #{self.shop.name}"
end
end
Now let's have an example:
at_store = Shop.new("in store", ["bulldozer", "Ducati"])
online = Shop.new("online", ["can opener", "Makita router"])
arr = [at_store, online]
We can do this:
arr.flat_map { |s| s.products }.each { |p| p.send(:uniq_code) }
# buy bulldozer in store
# buy Ducati in store
# buy can opener online
# buy Makita router online
Is this roughly what you were looking for?
Edit: to save the product instances for a given shop instance:
product_instances = at_store.products
#=> [#<Product:0x007f93f984f938
# #shop=#<Shop:0x007f93f984f988 #name="in_store",
# #products=[...]>, #name="bulldozer">,
# #<Product:0x007f93f984f910
# #shop=#<Shop:0x007f93f984f988 #name="in_store",
# #products=[...]>, #name="Ducati">]
for use later:
product_instances.each { |p| p.uniq_code }
# buy bulldozer in_store
# buy Ducati in_store
I am trying to figure out why my code below is not working. As you can see, the code is very brief and simple.
Could someone please help me understand why I am receiving this error message:
"NoMethodError: undefined method `<<' for nil:NilClass"
when I enter this command into terminal (after loading script):
mex_cuisine.add_recipe("charro beans")
The script:
class Cookbook
attr_accessor :title, :recipes, :recipe
def initialize(title)
#title = title
#recipes = []
end
def add_recipe(recipe)
#recipe = recipe
#recipes << #recipe
end
end
class Recipe
attr_accessor :name, :ingredients, :steps
def initialize(name, ingredients, steps)
#name = name
#ingredients = ingredients
#steps = steps
end
end
Your assistance is much appreciated.
Thank you.
**Edit:
Substituting this code:
def add_recipe(recipe)
#recipes.push(recipe)
puts "Added a recipe to the collection: #{recipe.title}"
end
causes the same error... Here is the long version:
NoMethodError: undefined method push' for nil:NilClass
from cookbook.rb:10:inadd_recipe'
from (irb):173
from /Users/patrickmeaney/.rvm/rubies/ruby-2.1.1/bin/irb:11:in `'
**Edit:
Here is the given test code:
mex_cuisine = Cookbook.new("Mexican Cooking")
burrito = Recipe.new("Bean Burrito", ["tortilla", "bean"], ["heat beans", "place beans in tortilla", "roll up"])
mex_cuisine.recipes # []
mex_cuisine.add_recipe(burrito)
Put the following code in a file called my_prog.rb:
class Cookbook
attr_accessor :title, :recipes, :recipe
def initialize(title)
#title = title
#recipes = []
end
def add_recipe(recipe)
#recipe = recipe
#recipes << #recipe
end
end
class Recipe
attr_accessor :name, :ingredients, :steps
def initialize(name, ingredients, steps)
#name = name
#ingredients = ingredients
#steps = steps
end
end
class Cookbook
attr_accessor :title, :recipes, :recipe
def initialize(title)
#title = title
#recipes = []
end
def add_recipe(recipe)
#recipe = recipe
#recipes << #recipe
end
end
class Recipe
attr_accessor :name, :ingredients, :steps
def initialize(name, ingredients, steps)
#name = name
#ingredients = ingredients
#steps = steps
end
end
mex_cuisine = Cookbook.new("Mexican Cooking")
burrito = Recipe.new("Bean Burrito", ["tortilla", "bean"], ["heat beans", "place beans in tortilla", "roll up"])
mex_cuisine.recipes # []
mex_cuisine.add_recipe(burrito)
Open a terminal window and cd to the same directory as my_prog.rb. Then run your program:
ruby my_prog.rb
There will be no errors.
Ok guys, I am learning ruby and I am having a little bit of trouble with the tutorial. I was wondering if you could help me out!
Take the following code:
class Dish
def initialize(name, ingred, descrip)
#name = name
#ingred = ingred
#descrip = descrip
end
def name
#name
end
def name=(new_name)
#name = new_name
end
def ingred
#ingred
end
def ingred=(new_ingred)
#ingred = new_ingred
end
def descrip
#descrip
end
def descrip=(new_descrip)
#descrip = new_descrip
end
def display
puts "I am a #{#name} and my ingredient is #{#ingred} and my description is #{descrip}"
end
end
dis1 = Dish.new('Pizza', 'sauce', 'put sauce on that thing')
dis1.display
Ok so here is my question and I hope I explain it well enough. So far I have learned to take enter one parameter when making a new instance of a class (i.e. (name, ingred, descrip)). What I am wondering is if a dish has multiple ingredients, how would I add that to my class? Also, if I wanted to count the number of ingredients or the number of names, how would I do that. I am just learning about classes and I am having trouble matching the exact wording I would Google for. Thanks!
I'll try to answer some of your questions. To simplify, I removed your variable, descrip and its associated methods. You see I've put a * in front of ingred in initialize. This means that a variable number of arguments are passed after name. This is one way of dealing with your question about having multiple ingredients. Here ingred is an array. Since #ingred is set equal to ingred, #ingred is also an array. If you look at the various methods and what some print when invoked (shown at the bottom), you should be able to see how this works. (Edited to add a bit of functionality. You may need to scroll down at the bottom.)
class Dish
def initialize(name, *ingred)
#name = name
#ingred = ingred
end
def name
#name
end
def name=(new_name)
#name = new_name
end
def ingred
#ingred
end
def ingred=(*ingred)
#ingred = ingred
end
def add_ingred(ingred)
#ingred << ingred
end
def remove_ingred(ingred)
#ingred.delete(ingred)
end
def nbr_ingred
#ingred.count
end
end
dis1 = Dish.new("Pizza", "sauce", "crust", "cheese", "anchovies")
p dis1.ingred #=> ["sauce", "crust", "cheese", "anchovies"]
dis1.add_ingred("olives")
p dis1.ingred #=> ["sauce", "crust", "cheese", "anchovies", "olives"]
dis1.add_ingred(["capers", "mushrooms"])
p dis1.ingred #=> ["sauce", "crust", "cheese", "anchovies", "olives", ["capers", "mushrooms"]]
dis1.ingred.flatten!
p dis1.ingred #=> ["sauce", "crust", "cheese", "anchovies", "olives", "capers", "mushrooms"]
dis1.remove_ingred("anchovies")
p dis1.ingred #=> ["sauce", "crust", "cheese", "olives", "capers", "mushrooms"]
p dis1.nbr_ingred #=> 6
dis1.ingred = "olives", "pineapple" # treated as ["olives", "pineapple"]
p dis1.ingred #=> [["olives", "pineapple"]]
dis1.ingred = ["cheese", "crust"]
p dis1.ingred #=> [["olives", "pineapple"]]
dis1.ingred.flatten!
p dis1.ingred #=> ["olives", "pineapple"]
Use arrays.
class Dish
class Ingredient
attr_accessor :name, :description
def initialize(name, description)
#name = name
#description = description
end
def to_s
"#{name} - #{description}"
end
end
attr_accessor :name, :description, :ingredients
def initialize(name, description)
#name = name
#description = description
#ingredients = []
end
def to_s
"#{name} - #{description}\n #{ingredients.join("\n").to_s}"
end
end
pizza = Dish.new("Pizza", "Italian style pizza")
pizza.ingredients << Dish::Ingredient.new("Tomato Sauce", "Juicy Juicy Tomato Sauce.")
pizza.ingredients << Dish::Ingredient.new("Cheese", "Cheese, duh.")
puts pizza.to_s
As the two answers before me did both leave out the description parameter i will stealCary Swoveland's answer and add the descrip parameter:
class Dish
attr_accessor :name, :descrip
def initialize(name, *ingred, descrip) # Only in Ruby 1.9+
#name = name
#ingred = *ingred
#descrip = descrip
end
def ingred
#ingred
end
def ingred=(*ingred)
#ingred = ingred
end
def add_ingred(ingred)
#ingred << ingred
end
def remove_ingred(ingred)
#ingred.delete(ingred)
end
def nbr_ingred
#ingred.count
end
def display
puts "I am a #{#name} and my ingredient is #{#ingred.join(', ')} and my description is #{descrip}"
end
end
dis1 = Dish.new('Pizza', 'sauce', 'ham', 'put ingredients on that thing.')
dis1.add_ingred('fish')
dis1.display #=> I am a Pizza and my ingredient is sauce, ham, fish and my description is put ingredients on that thing.