I encountered the following Ruby code:
class MyClass
attr_accessor :items
...
def each
@items.each{|item| yield item}
end
...
end
As a newbie, looking through a number of the answers confused me until I hit Abhi's answer.
the yield command pauses executing the code in the method, and instead passes control back to the block of code that called it, executes that code, and then continues executing the rest of the method after that. Here's an example that clarified it for me:
def hello
puts "hello"
yield
puts "world"
end
hello do
puts "there"
end
Output:
hello
there
world