Created
January 10, 2020 17:40
-
-
Save porras/aaa38cd66f67b6e258d2251342693524 to your computer and use it in GitHub Desktop.
Example of a usecase of Ruby 2.7 triple dot notation for writing method wrappers
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
def original_method(a, b) | |
puts "Called with #{a} and #{b}" | |
if block_given? | |
puts "Calling the block" | |
yield | |
end | |
end | |
def wrapper1(*args) # WRONG: doesn't pass the block | |
puts "Hi I'm wrapper1" | |
original_method(*args) | |
end | |
def wrapper2(*args, &block) # Works as expected, easy to forget | |
puts "Hi I'm wrapper2" | |
original_method(*args, &block) | |
end | |
def wrapper3(...) # Works as expected (Ruby >2.7) | |
puts "Hi I'm wrapper3" | |
original_method(...) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment