Paul Graham explains Ruby symbols

  • Thread starter Luciano Ramalho
  • Start date
J

joswig

Paul Graham offers this excellent explanation for the symbol type:
"Symbols are effectively pointers to strings stored in a hash table.
So you can test equality by comparing a pointer, instead of comparing
each character." [1]
Of course, he's talking about symbols inLisp, but what he says
applies equally well to Ruby and Smalltalk.

I find this a little too implementation-centric a description.

The key aspect of symbols is that two symbols are identical if they are equal.
The fact that there may (or may not be) a hash table used to ensure
this is irelevant.

And that description really misses the boat as far asLispis
concerned.Lispsymbols aren't strings, they are really names to which
three (separate) things can be bound, a value (which can be anyLisp
object), a function, and a property list. Actually the name is also
one of the slots in a symbol.

Actually it's five in Common Lisp: Value, Function, Name, Property
List, Package.
Note that inLispthe value of a Symbol is separate from it's name,
and two Symbols can have the same value, they just can't have the same
name.

No, that's wrong. Two different symbols can have the same name in
Common Lisp.

CL-USER 30 > (eq '#:foo '#:foo)
NIL

CL-USER 31 > (symbolp '#:foo)
T

CL-USER 32 > (symbol-name '#:foo)
"FOO"
So inLispa symbol is more like an entry in the table of global names.

No. In Lisp a symbol is a data structure with above five (virtual)
slots.
Symbols can exist without a 'table of names'. These 'table of names'
are called packages. A symbol can belong to a package. You can then
lookup the symbol via the package by its name. Again a symbol
can exist without being interned in a package.
Symbols in Ruby and Smalltalk are more alike than Symbols inLisp.

Smalltalk and Ruby symbols have unique 'values' which are also their 'names'.

These are a bit keyword symbols in Lisp. All symbols that belong to
the keyword package
have themselves as the value.

CL-USER 30 > (eq '#:foo '#:foo)
NIL

CL-USER 31 > (symbolp '#:foo)
T

CL-USER 32 > (symbol-name '#:foo)
"FOO"

CL-USER 33 > :foo
:FOO

CL-USER 34 > (symbol-package :foo)
#<The KEYWORD package, 0/4 internal, 9284/32768 external>

CL-USER 35 > (eq :foo (symbol-value :foo))
T

CL-USER 36 > (describe ':foo)

:FOO is a SYMBOL
NAME "FOO"
VALUE :FOO
FUNCTION #<unbound function>
PLIST NIL
PACKAGE #<The KEYWORD package, 0/4 internal, 9284/32768
external>
 
D

David A. Black

Hi --

As Obi-wan Kenobi would say, "that's (only) true from a certain point of view."

irb(main):002:0> :a + :b
NoMethodError: undefined method `+' for :a:Symbol
from (irb):2

;-)

Sure, though it's no more specifically a limitation of the
integer-like point than the string-like point :)


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 
C

Chad Perrin

Paul Graham offers this excellent explanation for the symbol type:

"Symbols are effectively pointers to strings stored in a hash table.
So you can test equality by comparing a pointer, instead of comparing
each character." [1]

Of course, he's talking about symbols in Lisp, but what he says
applies equally well to Ruby and Smalltalk.

I find this a little too implementation-centric a description.

Unfortunately, there's no way to differentiate a string literal from a
symbol in a definitive manner without brushing up against
implementation. At least, I haven't seen such a thing yet.

lets see

def is_symbol?(arg)
arg.object_id == arg.to_sym.object_id rescue false
end

. . which brushes up against implementation.
 
G

Giles Bowkett

And a single symbol can be represented by several different literals:
:alpha, :'alpha', :"alpha"

So you have to be careful about talking about unique literals.

It's a pity you can't do this, though.
?> ;
?> end
SyntaxError: compile error
(irb):5: syntax error, unexpected ';', expecting tSTRING_CONTENT or
tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
from (irb):6


Although I admit, wanting to do it in the first place, that's probably
just the Perl monkey in me. Besides, this works fine:
=> :kermit
 
G

Giles Bowkett

Silly rabbit.

=> :kermit

hahahaha
duh, whoops.

Well, just so I can say I contributed something to this discussion
other than my own brainlessness, I was just going through "Programming
Ruby" out of boredom and ran into this:

"Other languages...call symbols atoms." (pg 308 in what I think is edition 2)

Seems like an endorsement of the Lisp interpretation, although going
to that kind of trouble seems very un-Lispy.
 
R

Rick DeNatale

Paul Graham offers this excellent explanation for the symbol type:
"Symbols are effectively pointers to strings stored in a hash table.
So you can test equality by comparing a pointer, instead of comparing
each character." [1]
Of course, he's talking about symbols inLisp, but what he says
applies equally well to Ruby and Smalltalk.

I find this a little too implementation-centric a description.

The key aspect of symbols is that two symbols are identical if they are equal.
The fact that there may (or may not be) a hash table used to ensure
this is irelevant.

And that description really misses the boat as far asLispis
concerned.Lispsymbols aren't strings, they are really names to which
three (separate) things can be bound, a value (which can be anyLisp
object), a function, and a property list. Actually the name is also
one of the slots in a symbol.

Actually it's five in Common Lisp: Value, Function, Name, Property
List, Package.
Note that inLispthe value of a Symbol is separate from it's name,
and two Symbols can have the same value, they just can't have the same
name.

No, that's wrong. Two different symbols can have the same name in
Common Lisp.

CL-USER 30 > (eq '#:foo '#:foo)
NIL

CL-USER 31 > (symbolp '#:foo)
T

CL-USER 32 > (symbol-name '#:foo)
"FOO"
So inLispa symbol is more like an entry in the table of global names.

No. In Lisp a symbol is a data structure with above five (virtual)
slots.
Symbols can exist without a 'table of names'. These 'table of names'
are called packages. A symbol can belong to a package. You can then
lookup the symbol via the package by its name. Again a symbol
can exist without being interned in a package

Well, I never claimed to be a Lisp expert, the last time I used Lisp
seriously it was Lisp 1.5.

On the other hand, the author of the originally referenced
'explanation of symbols' seemed to be basing his description on his
knowledge of Lisp.

Given all the other differences pointed out by joswig, I think that my
original contention that:

seems to be more true than ever.
 
J

joswig

Paul Graham offers this excellent explanation for the symbol type:
"Symbols are effectively pointers to strings stored in a hash table.
So you can test equality by comparing a pointer, instead of comparing
each character." [1]
Of course, he's talking about symbols inLisp, but what he says
applies equally well to Ruby and Smalltalk.
I find this a little too implementation-centric a description.
The key aspect of symbols is that two symbols are identical if they are equal.
The fact that there may (or may not be) a hash table used to ensure
this is irelevant.
And that description really misses the boat as far asLispis
concerned.Lispsymbols aren't strings, they are really names to which
three (separate) things can be bound, a value (which can be anyLisp
object), a function, and a property list. Actually the name is also
one of the slots in a symbol.
Actually it's five in Common Lisp: Value, Function, Name, Property
List, Package.
No, that's wrong. Two different symbols can have the same name in
Common Lisp.
CL-USER 30 > (eq '#:foo '#:foo)
NIL
CL-USER 31 > (symbolp '#:foo)
T
CL-USER 32 > (symbol-name '#:foo)
"FOO"
No. In Lisp a symbol is a data structure with above five (virtual)
slots.
Symbols can exist without a 'table of names'. These 'table of names'
are called packages. A symbol can belong to a package. You can then
lookup the symbol via the package by its name. Again a symbol
can exist without being interned in a package

Well, I never claimed to be a Lisp expert, the last time I used Lisp
seriously it was Lisp 1.5.

Lisp 1.5 is not really in use anymore.
On the other hand, the author of the originally referenced
'explanation of symbols' seemed to be basing his description on his
knowledge of Lisp.

Given all the other differences pointed out byjoswig, I think that my
original contention that:


seems to be more true than ever.

Symbols in Ruby and Smalltalk are like keyword symbols in Common Lisp.
Keyword symbols are symbols in the package KEYWORD and they
have themselves as value and they are unique.


 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top