Paul Graham explains Ruby symbols

Hi –

On 3/8/07, Rick DeNatale [email protected] wrote:

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

:wink:

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

David

On Mar 7, 6:17 pm, “Rick DeNatale” [email protected] wrote:

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 #
PLIST NIL
PACKAGE #<The KEYWORD package, 0/4 internal, 9284/32768
external>

On Fri, Mar 09, 2007 at 06:11:43AM +0900, Rick DeNatale wrote:

applies equally well to Ruby and Smalltalk.
arg.object_id == arg.to_sym.object_id rescue false
end

. . . which brushes up against implementation.

Giles B. wrote:

It’s a pity you can’t do this, though.

:“kermit”

=> :kermit

:#{muppet}

Silly rabbit.

:"#{muppet}"
=> :kermit

On 3/8/07, Devin M. [email protected] wrote:

:“#{muppet}”
=> :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.

On 3/8/07, [email protected]
[email protected] wrote:

applies equally well to Ruby and Smalltalk.
object), a function, and a property list. Actually the name is also
Common Lisp.

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:

Symbols in Ruby and Smalltalk are more alike than Symbols inLisp.

seems to be more true than ever.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Mar 9, 3:13 pm, “Rick DeNatale” [email protected] wrote:

"Symbols are effectively pointers to strings stored in a hash table.
this is irelevant.
Note that inLispthe value of a Symbol is separate from it’s name,
T
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.

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.

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.

muppet = “kermit”
=> “kermit”

:kermit
=> :kermit

:“kermit”
=> :kermit

:#{muppet}
?> ;
?> 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:

muppet.to_sym
=> :kermit