The symbol dilemma...

M

Mayuresh Kathe

Hi,

There's one more thing, I also read the following statements;
---
Ruby's hashes can use any object as a key, but Symbol objects are the
most commonly
used. Symbols are immutable, interned strings. They can be compared by identity
rather than by textual content (because two distinct Symbol objects
will never have the
same content).
---

The statement, "because two distinct Symbol objects will never have
the same content" seems unclear to me.

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?

Thanks,

~Mayuresh
 
F

Farrel Lifson

2008/8/18 Mayuresh Kathe said:
The statement, "because two distinct Symbol objects will never have
the same content" seems unclear to me.

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.

Farrel
 
J

James Coglan

[Note: parts of this message were removed to make it a legal post.]
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.
 
J

Joost Diepenmaat

Mayuresh Kathe said:
The statement, "because two distinct Symbol objects will never have
the same content" seems unclear to me.

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?

No, :ugly refers to ("has the content of") the symbol :ugly, and :sick
refers to the symbol :sick. The fact that you're using different
symbols as keys to equal values in some hash is irreleveant. What's
meant by the original statement is:

There are no two symbols ! :x.equal?:)y) where :x.eql?:)y)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top