It’s good to understand how income works, but I rarely use it and thought the same is true for others. Comments on this answer may indicate otherwise.
The yield Ruby statement passes the control to the block given to the method. After the block is completed, the control returns to the method and continues executing the instruction immediately after exiting.
Here's a variant of the excessive 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
Then you 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
source share