Working my way through Ruby concepts right now. Coming from a VB background, there are some concepts I don't quite grasp yet. Yield is one of them. I understand how it works in a practical sense, but fail to see the significance of Yield, or when and how I would use it to its full potential.
3 Answers
Yield is part of a larger system of closures in Ruby. It is a very powerful part of the language and you will find it in every Ruby script you encounter.
http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/

- 3,963
- 2
- 21
- 40
-
Saving this for tomorrow morning, after my first caffeine shot! Thanks. – user973718 Sep 30 '11 at 22:23
It's good to have an understanding of how yield works but I seldom use it and thought that the same was true for others. The comments to this answer could indicate otherwise.
Ruby's yield statement hands over the control to a block given to the method. After the block has finished the control is returned to the method and it keeps on executing the statement directly after the yield.
Here's a variant of the overused Fibonacci sequence
def fib(upto)
curr, succ = 1, 1
while curr <= upto
puts "before"
yield curr
puts "after"
curr, succ = succ, curr+succ
end
end
You then call the method with something like
fib(8) {|res| puts res}
and the output will be
before
1
after
before
1
after
before
2
after
before
3
after
before
5
after
before
8
after

- 30,834
- 6
- 70
- 106
-
2"in practice you will almost never use it" - That strongly depends on what you do. We have quite a few places in our production app where we make use of `yield`. – Michael Kohl Sep 30 '11 at 20:33
-
I use code that uses yield quite often but I seldom write any yields myself. Guess I was way too subjective there. – Jonas Elfström Sep 30 '11 at 21:27
-
@JonasElfström Some examples: dependency injection. DSLs. The `yield self` pattern for something like the block configuration form in gemspecs. – Michael Kohl Sep 30 '11 at 21:37
-
@MichaelKohl You are clearly a very advanced Ruby programmer. As for DI I'm with Jamis Buck on that one http://stackoverflow.com/questions/283466/ruby-dependency-injection-libraries/283600#283600 http://weblog.jamisbuck.org/2008/11/9/legos-play-doh-and-programming and I also guess that's it's quite uncommon among Ruby programmers to write gems or even DSLs. I'm not picking a fight here. – Jonas Elfström Sep 30 '11 at 22:02
-
@JonasElfström: Didn't think you were and thanks for the links :-) I agree on the DI front btw, as said, that was a list of examples. – Michael Kohl Sep 30 '11 at 22:17
-
Thanks everyone for your input. This was a nice easy example of the workings of yield, thanks :-) – user973718 Sep 30 '11 at 22:22
-
@user973718 If it helped you then please check it as an accepted answer or upvote it and check another answer as accepted. – Jonas Elfström Sep 30 '11 at 23:55
Good reading: http://blog.codahale.com/2005/11/24/a-ruby-howto-writing-a-method-that-uses-code-blocks/

- 10,530
- 4
- 41
- 39