Hello,
I had to manipulate a hash (delete pairs, add pairs, )
But my problem is that the data comes sometimes as string and other
times as symbol, and this makes problems in updating the hash (sometimes
I have an entry 2 times with a symbol key and string key).
for example in this code:
hash = {:a => :b, 2 => 3, "g"=> 5 }
=> {2=>3, "g"=>5, :a=>:b}
h = {:a => :b, 2 => 4, :g=> 6 }
=> {:g=>6, 2=>4, :a=>:b}
hash.merge(h)
=> {:g=>6, 2=>4, "g"=>5, :a=>:b}
I want that the value of hash["g"] changes to 6 without adding another
pair {:g => 6}
If you have control of adding things to the hash you could call to_s (or to_sym) on it to force it into a string or symbol.
Hope this helps,
Henrik Hodne
···
On 9 Apr 2009, at 10:31, "Abir B." <abboura84@yahoo.fr> wrote:
Hello,
I had to manipulate a hash (delete pairs, add pairs, )
But my problem is that the data comes sometimes as string and other
times as symbol, and this makes problems in updating the hash (sometimes
I have an entry 2 times with a symbol key and string key).
for example in this code:
hash = {:a => :b, 2 => 3, "g"=> 5 }
=> {2=>3, "g"=>5, :a=>:b}
h = {:a => :b, 2 => 4, :g=> 6 }
=> {:g=>6, 2=>4, :a=>:b}
hash.merge(h)
=> {:g=>6, 2=>4, "g"=>5, :a=>:b}
I want that the value of hash["g"] changes to 6 without adding another
pair {:g => 6}
The easiest would be to decide which kind of key you really want
(String of Symbol), and ensure that you use only them.
Sorry for this a little bit sarcastic answer but Ruby's behavior is
perfectly valid here.
blambeau
···
On Thu, Apr 9, 2009 at 10:31 AM, Abir B. <abboura84@yahoo.fr> wrote:
Hello,
I had to manipulate a hash (delete pairs, add pairs, )
But my problem is that the data comes sometimes as string and other
times as symbol, and this makes problems in updating the hash (sometimes
I have an entry 2 times with a symbol key and string key).
for example in this code:
hash = {:a => :b, 2 => 3, "g"=> 5 }
=> {2=>3, "g"=>5, :a=>:b}
h = {:a => :b, 2 => 4, :g=> 6 }
=> {:g=>6, 2=>4, :a=>:b}
hash.merge(h)
=> {:g=>6, 2=>4, "g"=>5, :a=>:b}
I want that the value of hash["g"] changes to 6 without adding another
pair {:g => 6}
"I have never let my schooling interfere with my education" - Mark Twain
···
On Thu, Apr 9, 2009 at 10:31 AM, Abir B. <abboura84@yahoo.fr> wrote:
Hello,
I had to manipulate a hash (delete pairs, add pairs, )
But my problem is that the data comes sometimes as string and other
times as symbol, and this makes problems in updating the hash (sometimes
I have an entry 2 times with a symbol key and string key).
for example in this code:
hash = {:a => :b, 2 => 3, "g"=> 5 }
=> {2=>3, "g"=>5, :a=>:b}
h = {:a => :b, 2 => 4, :g=> 6 }
=> {:g=>6, 2=>4, :a=>:b}
hash.merge(h)
=> {:g=>6, 2=>4, "g"=>5, :a=>:b}
I want that the value of hash["g"] changes to 6 without adding another
pair {:g => 6}
new_hash = {}
hash.each { |k,v| new_hash[k.to_sym] = v }
h.each { |k,v| new_hash[k.to_sym] = v }
if we have nubers in the hash that we'll be converted to nil with to_sym
:
hash = {:a => :b, 2 => 3, "g"=> 5 }
=> {2=>3, "g"=>5, :a=>:b}
h = {:a => :b, 2 => 4, :g=> 6 }
=> {:g=>6, 2=>4, :a=>:b}
new_hash = {}
hash.each { |k,v| new_hash[k.to_sym] = v }
h.each { |k,v| new_hash[k.to_sym] = v }
if we have nubers in the hash that we'll be converted to nil with to_sym
:
hash = {:a => :b, 2 => 3, "g"=> 5 }
=> {2=>3, "g"=>5, :a=>:b}
h = {:a => :b, 2 => 4, :g=> 6 }
=> {:g=>6, 2=>4, :a=>:b}