2017-02-09 3 views
-1

"지침을"및 "재료"에서 반복되는 "Patch recipe.summary? ive"만 사용하여 값을 인쇄하지만 "조리법을 넣을 때"아무 것도 표시되지 않습니다. 요약 ". 여기 캔트 내 루비에서 배열의 값을 인쇄

어쩌면 잘못 통해 반복 메신저?

내 코드입니다. 당신은 @summary 반환하려고 후 puts를 호출

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

. 는 기술적으로 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 메서드 안에는 루비가 버리므로 그렇게하지 않을 것입니다. 그 코드를 실행할 때 당신은 SystemStackError: stack level too deep를 얻을 수 있습니다

attr_reader :name, :instructions,:ingredients 

... 

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

:

당신은 attr_reader으로 생성되는 메소드 이름으로 루비를 혼란 등과 같은 이름을 가진 내부 방법에서 그들을 호출하여 루프를 일으키는 원인이된다.

들여 쓰기를 자동으로 처리하는 편집기를 사용하거나 Rubocop과 같은 코드 분석기를 설치하고 종교적으로 사용하기 쉽도록 서식을 변경하는 것이 좋습니다.