This project is archived and is in readonly mode.

#421 ✓resolved
David Lowenfels

Hash with indifferent access reverse merge problem

Reported by David Lowenfels | June 15th, 2008 @ 06:23 AM

My business partner Rodney ran into this isse the other day.

http://blog.internautdesign.com/...

I think it is a bug, that violates the Principle of Least Surprise.

Loading development environment (Rails 2.1.0)
>> z = HashWithIndifferentAccess.new
=> {}
>> z.reverse_merge!( :a => 1 )
=> {:a=>1}
>> z.reverse_merge!( 'a' => 1 )
=> {"a"=>1, :a=>1}

Reverse merging on a HashWithIndifferentAccess should return a new HashWithIndifferentAccess, not a normal hash.

The solution is to overload HashWithIndifferentAccess#reverse_merge:

>> class HashWithIndifferentAccess
>>   def reverse_merge(other_hash)
>>     self.class.new( other_hash.merge(self) )
>>   end
>> end
=> nil
>> z = HashWithIndifferentAccess.new
=> {}
>> z.reverse_merge!( :a => 1 )
=> {"a"=>1}
>> z.reverse_merge!( 'a' => 1 )
=> {"a"=>1}
>> 

I'm attaching a patch with a test.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages