params[:hello] # => "Some params value"
hello = params[:hello]
hello.gsub!("whatever","")
params[:hello] # => ""
I don’t understand, can someone please explain why the params[:hello] gets modified by the gsub! ? I expected the hello string to be modified, but not the params hash.
helloandparams[:hello]are references to the same string. In ruby (as in java and python among others) assignment does not copy the value, it just stores another reference to the same value in the assigned-to variable. So unless you explicitly copy the string before modifying it (usingdup) any changes to the string will affect all other references to the string.