Forum: Ruby Hash key : string or symbol!?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2c917b23153c172ce386c0bd13ce54fd?d=identicon&s=25 Abir B. (abir84)
on 2009-04-09 10:30
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
4e39ce958e5ab660993b8b175b129803?d=identicon&s=25 Henrik Hodne (Guest)
on 2009-04-09 10:55
(Received via mailing list)
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
F1938de600a3feacd015051492fdaf0f?d=identicon&s=25 LAMBEAU Bernard (Guest)
on 2009-04-09 10:57
(Received via mailing list)
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
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-09 11:01
(Received via mailing list)
On Thu, Apr 9, 2009 at 10:31 AM, Abir B. <abboura84@yahoo.fr> wrote:
> 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 }


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

"I have never let my schooling interfere with my education" - Mark Twain
2c917b23153c172ce386c0bd13ce54fd?d=identicon&s=25 Abir B. (abir84)
on 2009-04-09 11:39
Andrew Timberlake wrote:

>> 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
4e39ce958e5ab660993b8b175b129803?d=identicon&s=25 Henrik Hodne (Guest)
on 2009-04-09 11:47
(Received via mailing list)
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
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-09 11:49
(Received via mailing list)
On Thu, Apr 9, 2009 at 11:40 AM, Abir B. <abboura84@yahoo.fr> wrote:
>>
>
> 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
This topic is locked and can not be replied to.