Example Hash
hash = {:key => ["val1", "val2]}
When I did this on rails 3.0.7, it was fine.
> hash.keys.to_s
=> "key"
> hash[hash.keys.to_s]
=> ["val1", "val2"]
But if I do this with rails 3.1.3, it isn't.
> hash.keys.to_s
=> [\"key\"]
> hash[hash.keys.to_s]
=> nil
Is this was because of the Rails version changed? and Is there any other way to turn hash key into a string that works with both version (or with rails 2 too)?
Did you upgrade Ruby as well as Rails? I think this is a change between 1.8 and 1.9
Try hash.keys.first.to_s
(if there's always only one key) or hash.keys.join
You simply need to convert it to a symbol instead of a string which is being more correct:
hash[hash.keys.to_sym]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With