In other words: the sequence :water always returns the same Symbol
instance while the sequence "water" (and 'water' also) create a new
instance all the time. If you have a limited set of Hash keys the
symbol is more appropriate because it is more efficient. If the
number of keys is large and changes often better use String (e.g. when
reading them from some external source).
Kind regards
robert
···
On Thu, Dec 9, 2010 at 9:08 AM, Soichi Ishida <soujiro0725+rubyforum@gmail.com> wrote:
Hi. I am a little confused with the use of ":" like,
Symbols are Ruby's implementation of string interning or pooling, where
all the distinct symbols, each of which being immutable, are "interned"
in a constant pool. This mechanism brings better performance (both in
time
and space) of string comparison and reduces memory usage, but also
increases the time to create strings.
One way of describing symbols is they are constants whose value is their
name.
As far as your example is concerned, the two options give you rather the
same effect. Most likely "water" is held as an object of type string
whereas :water is held as an entry in a symbol table. Quite possibly one
is more efficient that the other (either faster or taking less space)
but Ruby is not supposed to be a language that worries about such things
(it's supposed to be human oriented), and I'm not convinced you could
rely on that in all implementations.