Monday, 27 February 2012

Ruby: yield statement

A method can invoke an associated block one or more times using the Ruby yield statement. You can think of yield as being something like a method call that invokes the block associated with the call to the method containing the yield. Whenever yield is executed, it invokes the code in the block. When the block exits, control picks back up immediately after the yield. Here's a trivial example:
def three_times
  yield
  yield
  yield
end
three_times { puts "Hello" }
produces:
Hello
Hello
Hello

No comments :

Post a Comment