Skip to content

Instantly share code, notes, and snippets.

@porras
Created January 10, 2020 17:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porras/aaa38cd66f67b6e258d2251342693524 to your computer and use it in GitHub Desktop.
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
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