This project is archived and is in readonly mode.
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
-
DrMark June 26th, 2008 @ 03:14 AM
- Tag set to activesupport, bug, core_ext, patch
+1 This is rather unexpected behavior. Given that Ruby tries to do the least surprising thing, shouldn't we fix this?
-
Rodney Carvalho July 8th, 2008 @ 11:46 PM
+1 Yes, this caused me hours of frustration. Indifferent access should happen on both getting and setting.
-
Gregory Tomei July 23rd, 2008 @ 03:04 AM
this does seem to be inconsistent with how the HashWithIndifferentAccess class is intended to work.
-
H@rlan Knight July 23rd, 2008 @ 05:53 AM
+1 Agreed, this would be a serious improvement. The current behavior leads to difficult-to-trace bugs.
-
Brad Folkens October 27th, 2008 @ 03:10 PM
+1 Just ran into this problem this morning with some failing tests - unexpected behavior
-
Brad Folkens January 9th, 2009 @ 04:23 PM
- Tag changed from activesupport, bug, core_ext, patch to activesupport, bug, core_ext, patch, verified
I have a slightly different take on this now, some of my tests on my app started failing with the previous patch.
This updated version seems to work for additional cases but for some reason I can't get the activesupport tests to break (proving this update works). The problem seems to occur in the full rails stack when it is more than just the simple case being merged.
Solution is to convert the hash passed into reverse_merge into HashWithIndifferentAccess first, then merge.
-
Dmitry Ratnikov January 16th, 2009 @ 08:36 PM
I have modified the second patch a bit further by re-using the underlying implementation of reverse_merge rather than dublicating it again.
-
Repository March 12th, 2009 @ 03:14 PM
- State changed from new to resolved
(from [aa57e66fec3a131f5d246b8950a2c3286f858b78]) Ensure HWIA#reverse_merge! retrurns HWIA [#421 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
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>
People watching this ticket
Attachments
Referenced by
- 421 Hash with indifferent access reverse merge problem (from [aa57e66fec3a131f5d246b8950a2c3286f858b78]) Ensure ...