This example shows the thing I like about the Ruby language the most. It is simple but opens up subtle possibilities and works in a way that you don't have to think about. It works exactly as you would expect it to work. It could have worked the other way and Ruby would have suffered for it.

class Foo
  def m
    puts 'before yield'
    yield
    puts 'after yield'
  end

  def f
    puts 'calling m'
    m { puts 'in block'; return 'stuff'}
    puts 'done calling m'
  end
end

foo = Foo.new
r = foo.f

puts "method f returned: #{r}"

Running this program yields the following output:

$ ruby example.rb
calling m
before yield
in block
method f returned: stuff

When calling m and passing a block with a return statement, the return is done from the calling method f. Neither the 'after yield' nor the 'done calling m' statement is executed. This is exactly as it should work, but I can imagine other implementations where one or both of these statements were called. It wouldn't have been as beautiful, however.

As my wife is fond of saying, "It's the little things ..."! Thanks, Matz.