Ruby 2: You can rid options argument
Ruby 2 improved the support to keyword arguments. You can define your method with the default hash params as *args
, and rid the options = {}
. And no more need of options.reverse_merge(default_options)
.
Required arguments: you need a colon after the key (also you need ruby 2.1)
Optional arguments: just set the default to nil
Also you can use the standard positional args with this new hash parameters notation. You will find more information at this blog and at the official documentation.
Bonus: The refactor is easy because you can rid the options hash of your method without changing it’s callers. But… this is not completely true, if you have a call with an unexpected option you will get an error: ArgumentError: unknown keyword: <the_invalid_arg>
. And of course, calling with nil
parameter is not the same of calling without it, you’ll lose the default value because nil
is a valid one.