# Hash assignment \[:x\] vs \['x'\]

**URL:** <https://www.ruby-forum.com/t/hash-assignment-x-vs-x/168779>\
**Category:** Ruby\
**Created:** [May 25, 2009, 7:19pm UTC](https://www.ruby-forum.com/t/hash-assignment-x-vs-x/168779 "2009-05-25T19:19:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 25, 2009, 7:19pm UTC](https://www.ruby-forum.com/t/hash-assignment-x-vs-x/168779/1 "2009-05-25T19:19:06Z")

</div>

Being totally new to ruby, I just noticed that using

bob[‘phone’]

which coming from perl is a style I’m used to, results in a separate key  
than

bob[:phone]

That seems slightly weird, I had assumed the :form is just a shorthand  
like %w( ). So – is there anything more that I need to understand  
about this distinction?

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 25, 2009, 7:26pm UTC](https://www.ruby-forum.com/t/hash-assignment-x-vs-x/168779/2 "2009-05-25T19:26:57Z")

</div>

Mk 27 wrote:

> like %w( ). So – is there anything more that I need to understand  
> about this distinction?

:x is a Symbol object. ‘x’ is a String. You can make a symbol from a  
string with the intern method, and a string from a symbol with the to\_s  
method.

A symbol is a named number that Ruby guarantees will have the same value  
throughout your program. You pick the name, Ruby picks the number.

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 25, 2009, 7:30pm UTC](https://www.ruby-forum.com/t/hash-assignment-x-vs-x/168779/3 "2009-05-25T19:30:15Z")

</div>

Mk 27 wrote:

> Being totally new to ruby, I just noticed that using
> 
> bob[‘phone’]
> 
> which coming from perl is a style I’m used to, results in a separate key  
> than
> 
> bob[:phone]
> 
> That seems slightly weird, I had assumed the :form is just a shorthand  
> like %w( ). So – is there anything more that I need to understand  
> about this distinction?

Yes. :phone is a Symbol literal, whereas ‘phone’ is a String literal.

Symbols and Strings are two different types of object entirely. In  
particular:

- Symbols are immutable

- a literal like :foo will always give exactly the same object instance  
throughout the lifetime of the program, whereas a string literal like  
‘foo’ will give a fresh object every time it is used

For this reason, symbols are somewhat more efficient for hash lookups.  
Some libraries use them for passing hashes of options, although I  
suspect this is more because they are one character shorter to type,  
rather than any particular performance reason 🙂

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 25, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/hash-assignment-x-vs-x/168779/4 "2009-05-25T19:32:13Z")

</div>

On Mon, May 25, 2009 at 6:19 PM, Mk 27 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> like %w( ). So – is there anything more that I need to understand  
> about this distinction?

The first example is indexing via a string, the second is via a ruby  
symbol.  
You can think of a symbol as essentially a (very) lightweight string  
usually  
used for keying into hashes and general naming of things.

Rgds,  
Ben

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 25, 2009, 7:56pm UTC](https://www.ruby-forum.com/t/hash-assignment-x-vs-x/168779/5 "2009-05-25T19:56:54Z")

</div>

Hi,

Am Dienstag, 26. Mai 2009, 02:19:29 +0900 schrieb Mk 27:

> bob[‘phone’]  
> bob[:phone]
> 
> That seems slightly weird, I had assumed the :form is just a shorthand  
> like %w( ). So – is there anything more that I need to understand  
> about this distinction?

$ irb  
irb(main):001:0\> :phone.class  
=\> Symbol  
irb(main):002:0\> ‘phone’.class  
=\> String

and

irb(main):003:0\> :phone.methods.sort - ‘phone’.methods  
=\> [“id2name”, “to\_int”]  
irb(main):004:0\> String.instance\_methods.sort -  
Symbol.instance\_methods  
=\> [… (loads)]

:phone.methods == Symbol.instance\_methods

Bertram

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 25, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/hash-assignment-x-vs-x/168779/6 "2009-05-25T21:50:54Z")

</div>

On 25.05.2009 20:13, Rick DeNatale wrote:

> Strings are mutable, …

Well, most of the time - but sometimes they aren’t:

irb(main):001:0\> “I am not”.freeze.upcase!  
RuntimeError: can’t modify frozen string  
from (irb):2:in `upcase!' from (irb):2 from /usr/local/bin/irb19:12:in`’  
irb(main):002:0\>

😉

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 25, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/hash-assignment-x-vs-x/168779/7 "2009-05-25T20:13:55Z")

</div>

On Mon, May 25, 2009 at 1:56 PM, Bertram S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> $ irb  
> =\> [… (loads)]
> 
> :phone.methods == Symbol.instance\_methods

Strings are mutable, Symbols are immutable.

There is only one instance of a Symbol with a given string ‘value’ so

(hash1 == hash2) iff (hash1.object\_id == hash2.object\_id)

This is the main property of a Symbol, it’s used for keys in Hashes  
where lookup performance is important (like the method hashes used  
internally by Ruby), since it means that Hash#== is O(1) and doesn’t  
depend on the ‘length’

The downside is that Symbols can’t be garbage collected (at least for  
the MRI implementation) in order to preserve the id/value invariant a  
list of all string values which have been ‘interned’ when a Hash has  
been created needs to be kept.

In Rails, it’s common to see Symbol and String keys used  
‘interchangeably’, but this is only possible because Rails provides a  
HashWithIndifferentAccess class which actually uses string keys  
internally (because of the GC issue) but allows either to be used as  
key arguments to methods.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
