9.Create Your Own!


#1

https://www.codecademy.com/courses/learn-ruby/lessons/blocks-procs-and-lambdas/exercises/create-your-own-2

My code works, but I’m a little confused what my method yields to since there is not a ‘yield block’ as seen in previous exercises. My understanding is that when this code looks for a block to yield to and doesn’t find one, it finishes by running the method with my newly created Proc… When the method first encounters ‘yield’, it doesn’t jump to the block in my Proc, does it?



def greeter()
  yield
end

phrase = Proc.new {puts "Hello there!"}

greeter(&phrase)



#2

Hi! @methodsurfer83618 :smiley:
Well, since you called the method with the Proc as the argument, it automatically yielded that to the Ruby console for you.
It doesn’t work unless you call the greeter() with the Proc (converted to a block) as the argument.

Hope this simple explanation helps.


#3

Ok, cool. So then, does the ‘yield’ yield down to where the method greeter() is called?


#4

Exactly!! Keep up the good work. :smiley:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.