The code works. But besides that, there is an error. Why and how to fix it?

The code works. But besides that, there is an error. Why and how to fix it?

def make_casserole
    puts "Preheat oven to 375 degrees"
    ingredients = yield
    puts "Place #{ingredients} in dish"
    puts "Bake for 20 minutes"
end

make_casserole do 
  "noodles, celery, and tuna"
end

make_casserole do 
  "rice, broccoli, and chicken"
end

make_casserole

blocc.rb:31:in `make_casserole': no block given (yield) (LocalJumpError)from blocc.rb:45:in `<main>'

>Solution :

You must change the line ingredients = yield to ingredients = yield if block_given?. This is because you invoke the method without passing a block. The method cannot yield if no block given.

Leave a Reply