Writing a pass-through method, Ruby 2.7 edition

Prem Sichanugrist
Sikachu's Blog
Published in
1 min readMar 26, 2020

--

You may have written a Ruby method which pass all the arguments to the underlying method. For example:

def self.run(*args, &block)
new.run(*args, &block)
end

With the addition of keyword arguments in Ruby 2.0, things have gotten a bit crazier since you may want to pass all the keyword arguments to the underlying method as well. So, you’d want to update your method above to:

def self.run(*args, **opts, &block)
new.run(*args, **opts, &block)
end

However, starting from Ruby 2.7, you no longer need to do that as you can now use the new “forward everything” syntax. So, you can now rewrite the code above to look like this:

def self.run(...)
new.run(...)
end

… which is much cleaner than before!

If you would like to learn more about the development of this new syntax, you can look at the Redmine issue. Special thanks to Jean Boussier for submitting this pull request to Rails so I learned that this feature exist in Ruby 2.7.

--

--

Senior Developer at Degica. I also contribute to open source projects, mostly in Ruby.