James Coglan
8/18/2008 10:35:00 AM
[Note: parts of this message were removed to make it a legal post.]
>
> > Can't I do the following?
> > books = {
> > :excellent => "Iacocca",
> > :good => "Freakonomics",
> > :bad => "The World is Flat",
> > :ugly => "Guns, Germs and Steel",
> > :sick => "Guns, Germs and Steel"
> > }
> >
> > In that case, won't two distinct Symbol objects, i.e. :ugly, and :sick
> > have the same content?
> >
> > I'm sure there's something wrong in the way I'm understanding this,
> > could someone please elaborate?
>
> The symbols themselvs (:ugly and :sick) will always be distinct, not
> the strings they act as keys for in the hash.
More precisely, symbols with same value are shared and point to the same
objects in memory:
> syms = [:foo, :bar, :baz, :foo, :baz]
> syms.map { |s| s.object_id }
=> [148818, 161538, 161618, 148818, 161618]
So, if two symbols have the same value, they are actually the very same
object in memory. Therefore, two distinct Symbol objects must have different
values.