Skip to content

Instantly share code, notes, and snippets.

@mjackson
Created August 11, 2011 18:30
Show Gist options
  • Save mjackson/1140365 to your computer and use it in GitHub Desktop.
Save mjackson/1140365 to your computer and use it in GitHub Desktop.
Demonstrates the difference between Ruby's two different block styles.
def a(*args, &block)
puts "a got a block" if block_given?
end
def b(*args, &block)
puts "b got a block" if block_given?
end
# In this call, `b' is called with the block and the
# return value is given to `a' as an argument.
a b {}
# In this call, `a' gets the result of `b' *and* the block.
a b do
end
@raganwald
Copy link

To clarify, mjijackson is showing that teh Ruby parser binds {} more tightly than parameters, which bind more tightly than do...end. Excellent example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment