views:

41

answers:

1

I have an old function that is called many times in my application. I would like to update it a bit, which involves adding some new arguments.

When I wrote the function, I did not understand the benefits has making a hash the only parameter (mentioned here: http://www.skorks.com/2009/08/more-advanced-ruby-method-arguments-hashes-and-blocks/)

I have:

def old_function(some_arg)
 puts some_arg
end

I'd like to rewrite this to take a hash like so:

def old_function(some_arg, hash)
   puts hash[:new_stuff] + "--" + some_arg
end

All while this does not break:

puts old_function('greetings')
+4  A: 
def old_function(some_arg, hash = {})
  ## Code
end

The hash = {} sets the hash to an empty hash if none is given so older code will still pass through into the method.

railsninja