# Hash key : string or symbol!?

**URL:** <https://www.ruby-forum.com/t/hash-key-string-or-symbol/165117>\
**Category:** Ruby\
**Created:** [April 9, 2009, 10:30am UTC](https://www.ruby-forum.com/t/hash-key-string-or-symbol/165117 "2009-04-09T10:30:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [April 9, 2009, 10:30am UTC](https://www.ruby-forum.com/t/hash-key-string-or-symbol/165117/1 "2009-04-09T10:30:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [April 9, 2009, 10:55am UTC](https://www.ruby-forum.com/t/hash-key-string-or-symbol/165117/2 "2009-04-09T10:55:43Z")

</div>

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 H.

---

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [April 9, 2009, 10:57am UTC](https://www.ruby-forum.com/t/hash-key-string-or-symbol/165117/3 "2009-04-09T10:57:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [April 9, 2009, 11:01am UTC](https://www.ruby-forum.com/t/hash-key-string-or-symbol/165117/4 "2009-04-09T11:01:01Z")

</div>

On Thu, Apr 9, 2009 at 10:31 AM, Abir B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> h = {:a =\> :b, 2 =\> 4, :g=\> 6 }  
> Posted via [http://www.ruby-forum.com/](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 T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

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

---

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [April 9, 2009, 11:39am UTC](https://www.ruby-forum.com/t/hash-key-string-or-symbol/165117/5 "2009-04-09T11:39:53Z")

</div>

Andrew T. wrote:

> > h = {:a =\> :b, 2 =\> 4, :g=\> 6 }  
> > Posted via [http://www.ruby-forum.com/](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

---

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [April 9, 2009, 11:49am UTC](https://www.ruby-forum.com/t/hash-key-string-or-symbol/165117/6 "2009-04-09T11:49:49Z")

</div>

On Thu, Apr 9, 2009 at 11:40 AM, Abir B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

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

---

<div class="post-metadata">

**Author:** ![abir84](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@abir84](https://www.ruby-forum.com/u/abir84)\
**Post date:** [April 9, 2009, 11:47am UTC](https://www.ruby-forum.com/t/hash-key-string-or-symbol/165117/7 "2009-04-09T11:47:26Z")

</div>

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 H.
