Ruby Document

Hi TS,

I hope this is my last time chasing on this same question :slight_smile: First I want to
tell you is that the book you recommended is actually what I’m reading…

Just try it

pigeon% ruby -e ‘p :PropFind’
:PropFind
pigeon%

pigeon% ruby -e ‘p PropFind’
-e:1: uninitialized constant PropFind (NameError)
pigeon%

This only show the difference between symbol and name. But my key problem is
WHY we need symbol. What is it used FOR?

Thanks!
Shannon

···

MSN 8 with e-mail virus protection service: 2 months FREE*
http://join.msn.com/?page=features/virus

Hi Shannon,

pigeon% ruby -e ‘p :PropFind’
:PropFind
pigeon%

pigeon% ruby -e ‘p PropFind’
-e:1: uninitialized constant PropFind (NameError)
pigeon%

This only show the difference between symbol and name. But my key problem is
WHY we need symbol. What is it used FOR?

I don’t know how authoritative an answer I can present here, but:
I think symbols are neat. :slight_smile: Whenever you type a specific symbol
in your program, say, :some_symbol, you get back a single corresponding
instance of class Symbol. Take a look at the object id’s in the
following example:

:some_symbol.id
3309838
:some_symbol.id
3309838
“some_string”.id
22399992
“some_string”.id
22394712
“some_symbol”.intern
:some_symbol
“some_symbol”.intern.id
3309838

Hopefully the above illustrates clearly enough that when creating a
Symbol, there is a one-to-one mapping between the name of the symbol,
and the instance of class Symbol existing in Ruby to represent a
symbol of that name. . . . .

As far as what they’re used FOR, I’ve only arrived at that through
induction. A typical use of symbols in Ruby is to represent the
name of a method. For ex: “string”.respond_to? :intern #=> true.

Or, as you’ve noted, method-defining methods such as attr_reader,
etc… It’s convenient notation to represent the name of the method
with a symbol… You could make a method like attr_reader that
worked with strings as arguments instead of symbols, if you wanted
to… but attr_reader “myvar” just doesn’t look as nifty to me as
attr_reader :myvar… (I suspect, also, that the
representation of method names as symbols is connected to the
mechanism of method-name lookup internally in Ruby classes, but I
have not perused that part of the Ruby source to verify this…)

In my own code, for what it’s worth, I tend to use symbols in
places where I might have in other languages used an integer
constant to represent a (certain kind of) recurring named type-
value. Such as:

NOUN = 0
VERB = 1
ADVERB = 2
ADJECTIVE = 3

Instead, in Ruby, I’ll just go: if word.type == :noun …

Hope this helps,

:bill # :slight_smile:

···

From: “Shannon Fang” xrfang@hotmail.com

Hi Bill,

I get to know symbol more and more by talking with you ruby gurus… :slight_smile:
I know some of your examples, but still confusing :frowning:

“some_symbol”.intern
:some_symbol
This is what I don’t understand. “some_symobol” is a instant value,
without a name. why the system assign a symbole :some_symbol to it?
So if I have a string “This is a test” then the intern will be
:This_is_a_test? That’s sort of arbitrary…

Relating to your explaination, and previous messages, I still have some vital
question about understanding symbol:

  1. I tested using IRB, it seems that a symbol is not related to the name.
    For example,

irb(main):001:0> p :a.id
3602702
nil
irb(main):002:0> a=3
3
irb(main):003:0> p a.id
7
nil
irb(main):004:0> p :a.id
3602702
nil
irb(main):005:0>

It seems that I can create a symbol at any time, and it is not related
to the name. For example, after a=3, :a still point to the same place.
It seems that I have no way to use :a to reference to a? I am really
confused… :((

  1. I guessed some usage of symbol in your explanation. Please have a
    look at the following javascript code:

In the first alert, it will give out the name of the var you choose, but
the second will give out the value of the var you choose. I vaguely
guessed the use of symbol is related to this purpose. Can you implement
the above JavaScript code in Ruby? Is it related to the use of symbol?

Thanks a lot! Any ruby guru pls give me some hints!

Shannon

···

On Sun, 1 Dec 2002 04:05:36 +0900 “Bill Kelly” billk@cts.com wrote:

In my own code, for what it’s worth, I tend to use symbols in
places where I might have in other languages used an integer
constant to represent a (certain kind of) recurring named type-
value. Such as:

NOUN = 0
VERB = 1
ADVERB = 2
ADJECTIVE = 3

Instead, in Ruby, I’ll just go: if word.type == :noun …

I would do

NOUN = :NOUN
VERB = :VERB

if word.type == NOUN …

Your way is prone to evil spelling mistakes!

Gavin

···

From: “Bill Kelly” billk@cts.com

Hi Shannon,

“some_symbol”.intern
:some_symbol
This is what I don’t understand. “some_symobol” is a instant value,
without a name. why the system assign a symbole :some_symbol to it?
So if I have a string “This is a test” then the intern will be
:This_is_a_test? That’s sort of arbitrary…

“This is a test”.intern
:This is a test

It’s not arbitrary, it’s just that, using String#intern, we can
create symbols that we can’t represent in the source code using
the :name syntax. (Even though inspect will print them out that
way.)

Relating to your explaination, and previous messages, I still have some vital
question about understanding symbol:

  1. I tested using IRB, it seems that a symbol is not related to the name.
    For example,

irb(main):001:0> p :a.id
3602702
nil
irb(main):002:0> a=3
3
irb(main):003:0> p a.id
7
nil
irb(main):004:0> p :a.id
3602702
nil
irb(main):005:0>

It seems that I can create a symbol at any time, and it is not related
to the name. For example, after a=3, :a still point to the same place.
It seems that I have no way to use :a to reference to a? I am really
confused… :((

The trouble here would seem to be a confusion of the functionality of
the Symbol class, with actual symbols in the source code. They aren’t
related that way… All the Symbol class provides is essentially
similar to:

class SymbolLike
@@syms = {}
def initialize(str)
@name = str
end
def to_s
@name
end
def SymbolLike.lookup(str)
@@syms[str] ||= SymbolLike.new(str)
end
end
nil
a = SymbolLike.lookup(“sym1”)
#<SymbolLike:0x2aa5770 @name=“sym1”>
b = SymbolLike.lookup(“sym1”)
#<SymbolLike:0x2aa5770 @name=“sym1”>
a.id
22358968
b.id
22358968
a.to_s
“sym1”

…Just think of it as an invisible hash table behind the scenes
that ensures a single object instance of Symbol maps to any input
string of identical characters. It hasn’t any direct relation
(that I’m aware of) to variable names or any other identifiers in
the source code, except that the :name syntax is a short cut for
“name”.intern.

  1. I guessed some usage of symbol in your explanation. Please have a
    look at the following javascript code:

In the first alert, it will give out the name of the var you choose, but
the second will give out the value of the var you choose. I vaguely
guessed the use of symbol is related to this purpose. Can you implement
the above JavaScript code in Ruby? Is it related to the use of symbol?

Well… In Ruby I doubt whether I would see a use for symbols in
this example…

A variation might be:

class Test
def a; “3”; end
def b; “6”; end
end
nil

t = Test.new
#Test:0x2abbbf8

prompt = “‘which method do you want?’”, “t.send(:a)”
[“‘which method do you want?’”, “t.send(:a)”]

puts prompt.join(" ")
‘which method do you want?’ t.send(:a)
nil

puts prompt.map{|s| eval s}.join(" ")
which method do you want? 3
nil

More simply, without the eval, we could say:

method_you_want = :a
:a
puts "which method do you want? " + t.send(method_you_want)
which method do you want? 3
nil

Hope this helps,

Bill

···

On Sun, 1 Dec 2002 04:05:36 +0900 > “Bill Kelly” billk@cts.com wrote:

“Gavin Sinclair” gsinclair@soyabean.com.au writes:

I would do

NOUN = :NOUN
VERB = :VERB

if word.type == NOUN …

Your way is prone to evil spelling mistakes!

If you do that, is there a reason not to take the next step?

if word.type == :NOUN

Cheers

Dave

Instead, in Ruby, I’ll just go: if word.type == :noun …

I would do

NOUN = :NOUN
VERB = :VERB

if word.type == NOUN …

Your way is prone to evil spelling mistakes!

If I ever made such mistakes, I might argee.

( ;-D )

···

From: “Gavin Sinclair” gsinclair@soyabean.com.au

Hi Bill,

the source code, except that the :name syntax is a short cut for
“name”.intern.
Trouble is, :name is arbitrary, as I tested in irb. It seems when you
type :name, it is dynamically created in the system. And if in the
system there are a variable called name defined, you can not reference
to name through :name. Is it correct? If so, it is hard for me to think
of any use of symbol, except that it may save some bytes of memory.
Please see below.

prompt = “‘which method do you want?’”, “t.send(:a)”
[“‘which method do you want?’”, “t.send(:a)”]

Now is the problem makes my brain a mess. When you use t.send(:a), how
can the interpreter know to map :a to method a?? As I experimented, :a
can exist independent of a, and even there is a exists in the system,
you can’t use :a to reference a. p :a will output :a, not a, or value of
a! If :a is independent to any other variable, it can only be a
“immutable” or very economic string, what’s the big deal? Can’t you use
string.freeze to make it immutable (if it is really necessary)?

Please help… :cry:

Shannon

···

On Sun, 1 Dec 2002 06:47:06 +0900 “Bill Kelly” billk@cts.com wrote:

This test would always test false (because of the
misspelling):

if word.type == :NUON

Otherwise it would be caught as an error. That’s
what I think he means.

Hal

···

----- Original Message -----
From: “Dave Thomas” Dave@PragmaticProgrammer.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Sunday, December 01, 2002 10:18 AM
Subject: Re: Ruby Document

“Gavin Sinclair” gsinclair@soyabean.com.au writes:

I would do

NOUN = :NOUN
VERB = :VERB

if word.type == NOUN …

Your way is prone to evil spelling mistakes!

If you do that, is there a reason not to take the next step?

if word.type == :NOUN

Hi Shannon,

If :a is independent to any other variable, it can only be a
“immutable” or very economic string, what’s the big deal?

Yes, exactly. :a is not any kind of variable at all, nor is it
bound to any kind of variable. It, indeed, is an immutable,
economic, singleton construction of the string “a”.

What’s the big deal? Well, … does something have to be a Big
Deal to be useful? :slight_smile:

I like symbols in Ruby a lot. Smalltalk has them (but my
Smalltalk is rusty so I don’t remember it there’s a special
syntax for them); and Java has them
( Oracle Java Technologies | Oracle )
but what I think is so fantastic in Ruby is the ability to
specify a symbol syntactically without cluttering up the
code. :name is so much nicer and easier to read than
“name”.intern… So with Ruby we can utilize symbols for
certain kinds of constants, and, I think make the code very
readable.

@state = :login

. . . . .

case @state
  when :login  then session_handle_login(line)
  when :passwd then session_handle_passwd(line)
  when :shell  then session_handle_shell(line)
end

So maybe no big deal, but, I think Ruby would be diminished if it
didn’t have symbols so syntactically convenient…

Regards,

Bill

Now is the problem makes my brain a mess. When you use t.send(:a), how
can the interpreter know to map :a to method a??

Pretty simple, really: magic.

Do you accept that the interpreter can handle this?

t.send(‘a’)

It’d be a pretty piss-poor interpreter if it couldn’t. And since
(:a).to_s == ‘a’
the interpreter can also handle t.send(:a)

Cheers,
Gavin

···

From: “Shannon Fang” xrfang@hotmail.com

Precisely. Since symbols autovivify, there’s no error-catching mechanism if
you compare against them directly. One of the erasons you wouldn’t use literal
strings for the comparison.

Gavin

···

----- Original Message -----
From: “Hal E. Fulton” hal9000@hypermetrics.com

This test would always test false (because of the
misspelling):

if word.type == :NUON

Otherwise it would be caught as an error. That’s
what I think he means.

Hal