Skip to content

Instantly share code, notes, and snippets.

@timuruski
Last active September 8, 2023 19:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timuruski/ad2d5c5176719061c1e92bd321781193 to your computer and use it in GitHub Desktop.
Save timuruski/ad2d5c5176719061c1e92bd321781193 to your computer and use it in GitHub Desktop.
Demonstration of block binding differences in Ruby
class Expectation
def to(matcher = nil, &block)
if block_given?
puts "block passed to `to` method"
yield
end
end
end
def expect
Expectation.new
end
def have_logged(&block)
if block_given?
puts "block passed to `have_logged` method"
yield
end
end
expect do
# This will bind to `have_logged`
end.to have_logged {
puts "called with { }"
}
# block passed to `have_logged` method
# called with { }
expect do
# This will bind to `to`
end.to have_logged do
puts "called with do/end"
end
# block passed to `to` method
# called with do/end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment