2017-02-09 9 views
-1

「レシピを載せておいてください」という使い方を使って値を印刷するにはどうすればよいですか?概要」。ルーンの配列から値を印刷する

はたぶん間違ってそれをループイム? ここに私のコードです。

class Ingredient 
    attr_reader :quantity, :unit, :name, :summary 
    def initialize (quantity,unit,name) 
    @quantity = quantity 
    @unit = unit 
    @name = name 
    @summary= summary 
    end 

    def summary 
    "#{quantity} #{unit} #{name}" 
    end 
end 




class Recipe 
    attr_reader :name, :instructions,:ingredients 

    def initialize(name,instructions,ingredient) 
    @name = name 
    @instructions = instructions 
    @ingredient = ingredient 
    end 

    def instructions 
@instructions= instructions.each do |instruction| 
    puts instruction 
    end 
end 

    def ingredients 

    @ingredients = ingredients.each do |ingredient| 
     puts ingredient 
    end 


    def summary 
    @summary 
    puts "Name: #{name}" 
    puts "#{ingredients}" 
    end 
end 
end 

# ingredient = Ingredient.new(47.0, "lb(s)", "Brussels Sprouts") 
# puts ingredient.summary 

name = "Roasted Brussels Sprouts" 

instructions = [ 
    "Preheat oven to 400 degrees F.", 
    "Cut off the brown ends of the Brussels sprouts.", 
    "Pull off any yellow outer leaves.", 
    "Mix them in a bowl with the olive oil, salt and pepper.", 
    "Pour them on a sheet pan and roast for 35 to 40 minutes.", 
    "They should be until crisp on the outside and tender on the inside.", 
    "Shake the pan from time to time to brown the sprouts evenly.", 
    "Sprinkle with more kosher salt (I like these salty like French fries).", 
    "Serve and enjoy!" 
] 

ingredients = [ 
    Ingredient.new(1.5, "lb(s)", "Brussels sprouts"), 
    Ingredient.new(3.0, "tbspn(s)", "Good olive oil"), 
    Ingredient.new(0.75, "tspn(s)", "Kosher salt"), 
    Ingredient.new(0.5, "tspn(s)", "Freshly ground black pepper") 
] 


recipe = Recipe.new(name, instructions, ingredients) 
puts recipe.summary 
+0

「[mcve]」とお読みください。問題を再現する最小限のコードに減らしてください。 –

答えて

0

あなたが@summary返すようにしようとした後putsを呼び出している。 技術的puts "#{ingredients}"の結果は、返されるものれていますおそらく何もありません。
上記のputsを移動してください。

def ingredients 

    @ingredients = ingredients.each do |ingredient| 
     puts ingredient 
    end 


    def summary 
    @summary 
    puts "Name: #{name}" 
    puts "#{ingredients}" 
    end 
end 
ingredients方法外 summary方法を動かし

:あなたのネスティングに注意を払っていなかったので

def summary 
    puts "Name: #{name}" 
    puts "#{ingredients}" 
    @summary 
end 
+0

あなたの言うことが分かりますが、それでもそれ以降は何も印刷しません。 – Tiago

1

レシピオブジェクトがsummaryメソッドを持っていません。

@summarysummaryの方法ではRubyがそれを捨ててしまいます。そのコードを実行しているときは、SystemStackError: stack level too deepを取得します

attr_reader :name, :instructions,:ingredients 

... 

    def instructions 
    @instructions= instructions.each do |instruction| 
    puts instruction 
    end 

あなたはattr_readerで生成されたメソッド名とルビーを混乱や、同じ名前の内部メソッド、からそれらを呼び出すことにより、ループを引き起こしています。

字下げを自動的に処理するエディタを使用するか、Rubocopのようなコードアナライザをインストールして宗教的に使用しやすいように、Rubyコードのチュートリアルを作成することをおすすめします。