From: Brian Candler Date: 2009-05-26T02:30:19+09:00 Subject: Re: hash assignment [:x] vs ['x'] 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 :-) -- Posted via http://www.ruby-forum.com/.