>> [1, 2, 3].do_stuff
=> Result I get
>> [1, 2, 3].do_stuff :an_option => a_value
=> Result I really want, but don't want to specify the argument
I like to use superfor this. This allows us to add some functions to the method, except as only changing the default arguments:
class Array
def do_stuff(options = {})
options[:argument_i_want_to_change] = default_value_i_want unless options.has_key? :argument_i_want_to_change
super
end
end
Result:
>> [1, 2, 3].do_stuff
=> Result that I really want
UPDATE: removed reverse_merge! dependence. (Now looking for better alternatives to using the [] = method)
source
share