My code looks something like this
class MyController
def my_action
#params hash gets passed here
...
logger.debug "PARAMS >> #{params.inspect}" #first inspect
@a = MyModule::MyClass.new(params).my_func #some operations done with the argument
logger.debug "PARAMS >> #{params.inspect}" #second inspect
end
end
My problem is, the params hash in the ‘second inspect’ is different from the one in the ‘first inspect’. When the params hash is passed in the class’ initializer, some more key value pairs added to it, and when I do the ‘second inspect’ on params I find the same key value pairs in it too! How is this possible? I even tried passing a separate variable that contains the action’s params hash, but in vain!
first inspect : {"action"=>"report", "controller"=>"member/monitoring", "offset"=>"0"}
second inspect : {"new_key1"=>"new_val1", "action"=>"report", "controller"=>"member/monitoring","new_key2"=>"new_val2", "new_key3"=>"new_val3", "offset"=>"0"}
The new_key* are added inside the class, but they turn up in the action’s params hash too.
Values passed as parameters to a method are by reference in Ruby, not by value. As such, they can be descructive and can change your hash. You can easily fix this by using
params.dupas the argument to yournewmethod.Edit: As mu pointed out, it is more appropriate to actually let the
newmethod itself do thedup, instead of duping before passingparams.