-1

I am writing a simple cookbook app in Ruby, and I am stuck. It's a practice module that I am working on. Here are the instructions for the current ste

"Implement the following methods so that the added code works. The comments next to the code represent the output we want to see:"

mex_cuisine.recipe_titles # Veggie Burrito

mex_cuisine.recipe_ingredients # These are the ingredients for Veggie Burrito: ["tortilla", "bean"]

Hint: You have an @recipes array that has all the recipe objects in our cookbook object. Also, @recipes array which means it has elements to iterate through to grab their values.

I don't know how to grab certain values from @reciepes

I tried to add this method to the Cookbook Class:

def recipe_titles
          @recipes.each do |i|
             puts i
          end
end

I tired this to no avail. I had an error message on irb that read:

NoMethodError: undefined method `recipes_titles' for #<Cookbook:0x007fe1ea232ac0>

I didn't even attempt the second method .recipe_ingredients

Here is my code without the attempted method that I wrote above:

Cookbook = Class.new

Recipe = Class.new

class Cookbook
    attr_accessor :title
    attr_reader :recipes

    def initialize(title)
        @title = title
        @recipes = []
    end

    def add_recipe(recipe)
        @recipes.push(recipe)
          puts "Added a recipe to the collection: #{recipe.title}"
    end

end

class Recipe
    attr_accessor :title
    attr_accessor :steps
    attr_accessor :ingredients

    def initialize(title, ingredients, steps)
        @title = title
        @ingredients = ingredients
        @steps = steps
    end
end

And here is my test code:

require_relative 'cookbook'

mex_cuisine = Cookbook.new("Mexican Cooking")
burrito = Recipe.new("Bean Burrito", ["tortilla", "bean"], ["heat beans", "place beans       in tortilla", "roll up"])

puts mex_cuisine.title
puts burrito.title
p burrito.ingredients
p burrito.steps

mex_cuisine.title = "Mexican Recipes"
puts mex_cuisine.title

burrito.title = "Veggie Burrito"
burrito.ingredients = ["tortilla", "tomatoes"]
burrito.steps = ["heat tomatoes", "place tomatoes in tortilla", "roll up"]

p burrito.title
p burrito.ingredients

mex_cuisine.recipes
mex_cuisine.add_recipe(burrito)
p mex_cuisine.recipes

mex_cuisine.recipes_titles   # Veggie Burrito 


mex_cuisine.recipe_ingredients    # These are the ingredients for Veggie Burrito:   ["tortilla", "bean"]

The "Hint" that I wrote above was supposed to help me, but it confused me. Here it is again.

Hint: You have an @recipes array that has all the recipe objects in our cookbook object. Also, @recipes array which means it has elements to iterate through to grab their values.

Any ideas would be appreciated. I"m a newbie at Ruby, and I've been stuck on this for hours.

I hope that I am being clear, If I am not...I apologize in advance.

1 Answers1

1

Use map on the cookbook recipes.

class Cookbook
  # rest of class....

  def recipe_titles
    recipes.map { |recipe| recipe.title }
  end
end

This will give you an array of the recipe titles. If you want that as a string you can use join.

Jay Mitchell
  • 1,230
  • 11
  • 14
  • Thanks again JM.....I took your advice and tweaked it a bit to get my answer without quotations....which is what they requested. recipes.map {|recipe| puts "#{recipe.title}"} – user3843070 Sep 14 '14 at 17:39