Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

HashWithIndifferentAccess class [] operator bug #5551

Closed
Aalanar opened this Issue Mar 22, 2012 · 0 comments

Comments

Projects
None yet
2 participants

Aalanar commented Mar 22, 2012

looks like the class [] operator is just using the Hash inherited version without doing any key/value conversions

E.G.

using ruby 1.8.7
Loading development environment (Rails 3.1.3)

irb(main):001:0> h = HashWithIndifferentAccess[:foo, 1]
=> {:foo=>1}
irb(main):002:0> h[:foo] = 3
=> 3
irb(main):003:0> h[:foo]
=> 1
irb(main):004:0> h["foo"]
=> 3
irb(main):005:0> h
=> {:foo=>1, "foo"=>3}

a quick workaround I found

class HashWithIndifferentAccess
  def self.[](*args)
     new.merge(Hash[*args.to_a.flatten(1)])
  end
end

before

irb(main):001:0> HashWithIndifferentAccess[[[:foo, 1]]]
=> {:foo=>1}
irb(main):002:0> HashWithIndifferentAccess[{:foo => 1}]
=> {:foo=>1}
irb(main):003:0> HashWithIndifferentAccess[:foo, 1]
=> {:foo=>1}

after

irb(main):001:0> HashWithIndifferentAccess[[[:foo, 1]]]
=> {"foo"=>1}
irb(main):002:0> HashWithIndifferentAccess[{:foo => 1}]
=> {"foo"=>1}
irb(main):003:0> HashWithIndifferentAccess[:foo, 1]
=> {"foo"=>1}

@drogus drogus closed this Mar 25, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment