Hash key : string or symbol!?

A

Abir B.

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}

Someone has any idea?
thanks
 
H

Henrik Hodne

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
 
L

LAMBEAU Bernard

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
 
A

Andrew Timberlake

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}

Someone has any idea?
thanks

new_hash = {}
hash.each { |k,v| new_hash[k.to_sym] = v }
h.each { |k,v| new_hash[k.to_sym] = v }


Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
 
A

Abir B.

Andrew said:
h = {:a => :b, 2 => 4, :g=> 6 }
Posted via http://www.ruby-forum.com/.

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 }
=> {2=>3, "g"=>5, :a=>:b}
h.each { |k,v| new_hash[k.to_sym] = v }
=> {:g=>6, 2=>4, :a=>:b}
hash
=> {2=>3, "g"=>5, :a=>:b}
h
=> {:g=>6, 2=>4, :a=>:b}
new_hash
=> {:g=>6, nil=>4, :a=>:b}

And I can't control the inputs in the hash, so I must treat that by
myself
 
H

Henrik Hodne

You could still loop through them and make them into strings instead
of symbols.

new_hash = {}
h.each { |k,v| new_hash[k.to_s] = v }

Regards,
Henrik Hodne

Andrew said:
h = {:a => :b, 2 => 4, :g=> 6 }
Posted via http://www.ruby-forum.com/.

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 }
=> {2=>3, "g"=>5, :a=>:b}
h.each { |k,v| new_hash[k.to_sym] = v }
=> {:g=>6, 2=>4, :a=>:b}
hash
=> {2=>3, "g"=>5, :a=>:b}
h
=> {:g=>6, 2=>4, :a=>:b}
new_hash
=> {:g=>6, nil=>4, :a=>:b}

And I can't control the inputs in the hash, so I must treat that by
myself
 
A

Andrew Timberlake

Andrew said:
h = {:a => :b, 2 => 4, :g=> 6 }
Posted via http://www.ruby-forum.com/.

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
:
Then you could use
new_hash[k.kind_of?(Numeric) k : k.to_sym] = v

Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top