Last active
September 8, 2023 19:46
-
-
Save timuruski/ad2d5c5176719061c1e92bd321781193 to your computer and use it in GitHub Desktop.
Demonstration of block binding differences in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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