What is the difference between :foo and "foo"?

Christian N.:

Malte M.:

My understanding of things so far was that :sym and 3 are immediate
values, and after writing
a, b = :sym, 3
a is a reference to :sym and b is a reference to 3.
In the current implementation, no. It doesn’t matter to the programmer,
though.

I’m happy I’m not forced to dig into the details of Ruby’s immediate
values…

Malte

Steve L. wrote:

truer to the implementation, but when actually using the little bugger either
explanation will lead to correct programming, at least if I understand it
correctly (and I think I now understand it correctly, at least correctly
enough to know how and when to use a symbol).

I’m somewhat new to ruby, and for me it has made most sense to do an ri
on Symbol to check out the api. When I first ran across symbols in the
Pickaxe it didn’t make much sense. I started out just taking it on
blind faith how they get used in different frameworks api’s. Seems
like what symbols are is different from how they might be best used.
Here’s something I wrote up to play around with how they work. I was
surprized to find out that any identifier (class, method, variable,
etc) in a ruby script gets turned into a symbol.

Bill

Definition: A symbol is an object of class Symbol. The symbol

has only one instance with given name that can be found by

calling the method id2name or to_s. The symbol has a unique

integer value that can be found by calling the to_i method.

The Symbol class method all_symbols will give an array of all

symbols. A symbol is automatically generated for any

identifier used or seen within a ruby program including

constants, classes, methods, variables, etc. Once a symbol is

created it is never garbage collected.

def find_and_display_symbol(symbol_name)
found_symbol = Symbol.all_symbols.find { |s| s.to_s == symbol_name }
if (found_symbol != nil)
puts "symbol found with name: #{found_symbol.to_s} and " +
“id: #{found_symbol.to_i}\n\n”
else
puts “unable to find symbol #{symbol_name}\n\n”
end
end

create a symbol object called symbol_name

:my_new_symbol

display the newly created symbol by going through all symbols

see find_and_display_symbol function below

find_and_display_symbol(‘my_new_symbol’)

symbols apparently don’t have to be a legal identifier

:“Hello World!”
find_and_display_symbol(“Hello World!”)

#local variable creates a symbol
silly_local_variable = “Hello”
find_and_display_symbol(‘silly_local_variable’)

Any referenced identifier will create a symbol, even if the symbol is

undefined:

defined? undefined_variable
find_and_display_symbol(‘undefined_variable’)

Symbols already exist for builtin classes and methods

find_and_display_symbol(‘String’)
find_and_display_symbol(‘each’)
find_and_display_symbol(’$LOAD_PATH’)

list all symbols

sym_array = Symbol.all_symbols
sym_array.sort! {|a,b| a.to_s <=> b.to_s }
sym_array.each {|s| puts “name: #{s.to_s} id: #{s.to_i}\n” }

On Sat, 31 Dec 2005 20:10:13 -0000, [email protected] wrote:

Definition: A symbol is an object of class Symbol. The symbol

has only one instance with given name that can be found by

calling the method id2name or to_s. The symbol has a unique

integer value that can be found by calling the to_i method.

The Symbol class method all_symbols will give an array of all

symbols. A symbol is automatically generated for any

identifier used or seen within a ruby program including

constants, classes, methods, variables, etc. Once a symbol is

created it is never garbage collected.

S’funny, my RI says:

  +Symbol+ objects represent names and some strings inside the Ruby
  interpreter. They are generated using the +:name+ and +:"string"+
  literals syntax, and by the various +to_sym+ methods. The same
  +Symbol+ object will be created for a given name or string for the
  duration of a program's execution, regardless of the context or
  meaning of that name. Thus if +Fred+ is a constant in one context,
  a method in another, and a class in a third, the +Symbol+ +:Fred+
  will be the same object in all three contexts.

But anyway,

create a symbol object called symbol_name

#local variable creates a symbol
find_and_display_symbol(‘each’)
find_and_display_symbol(‘$LOAD_PATH’)

list all symbols

sym_array = Symbol.all_symbols
sym_array.sort! {|a,b| a.to_s <=> b.to_s }
sym_array.each {|s| puts “name: #{s.to_s} id: #{s.to_i}\n” }

Hey, that could be a good basis for a nuby file for my collection
(http://roscopeco.co.uk/code/noob), maybe as part of an expanded version
of the existing syms-methods thing (which isn’t too hot anyway to be
honest). Mind me doing that?

Cheers,

did
a, b = 4, 4
Basically I suppose I’m confused about assignment by value or reference. I
though Ruby was pass by reference, with references passed by value, no
matter what?

The ‘pass by reference/value’ terminology isn’t used much in Ruby (or
in Smalltalk, Java, Python…), from the programmer’s point of view
there’s only one type of assignment and parameter passing.

But technically, a variable can hold either a reference to an object
or an immediate value. All normal objects are assigned by reference,
so in
a=Object.new
or
a=“some string”
‘a’ holds a reference. The reference is a pointer to the object’s
location in memory.

Fixnums and a few other special types (symbols, true/false/nil,
floats?) are assigned as immediate values: Instead of storing a
pointer (or reference) to the value object, the variable stores the
value directly. So in
a=4
‘a’ does not hold a reference, technically speaking, but rather the
immediate value 4.
This is an implementation issue, and is done for efficiency.

But the difference is largely transparent to the programmer, so I
guess it’s mostly of interest to those who want to know how the
language works behind the scenes.

jf

James B. wrote:

After trying to follow all this, I’ve come to see Symbols as being more
akin to numbers than strings.

They’re numbers with a human face.

Thanks, James. That’s great.

Incidentally, I was out of town for over a week, so I have just now
read (most of) this 100+ post thread in a single sitting. This is
not an experience I recommend.

I had to reply here, though I held back many times.

If you are an old enough user of Ruby, you remember when a symbol
“really was” a number. There was no Symbol class. If you did a
puts of :foo, you got a number out. :foo could be added to another
number, and basically always acted like a number.

I’m assuming that this number was just the index into a list of
symbols, enabling us to get the string representation back given
the symbol (number).

Anyway, happy new year! This is my first post of 2006.

Hal

On 1/1/06, Hal F. [email protected] wrote:

Incidentally, I was out of town for over a week, so I have just now
read (most of) this 100+ post thread in a single sitting. This is
not an experience I recommend.

I had to reply here, though I held back many times.

If you are an old enough user of Ruby, you remember when a symbol
“really was” a number. There was no Symbol class. If you did a
puts of :foo, you got a number out. :foo could be added to another
number, and basically always acted like a number.

That’s interesting. What version did the Symbol class get added in?

Not at all. Use whatever parts you would like.

Bill

A symbol in Ruby is similar to a symbol in Lisp in that the symbol is a
name, or more precisely a named atom which can also respond to a
request to expose itself as a number (to_i) or a string (to_s). In this
case a Ruby symbol is similar to a Lisp keyword symbol (which is also
prefixed by a colon (:), at least in some versions of Lisp). However,
in Lisp a symbol has much greater functionality. For example, a Lisp
symbol can also be bound to a value, a property list, or a function.

In briefest, my understanding of symbol in Ruby is that it is an
interned string used as a name (“interned” meaning that it is entered
into memory as a single, atomic entity, a monad - there is just one of
it, with a single pointer to it).

Gregory B. wrote:

Thanks, James. That’s great.
number, and basically always acted like a number.

That’s interesting. What version did the Symbol class get added in?

I’m not certain. I guess it was 1.6.

Hal

James B. wrote:

After trying to follow all this, I’ve come to see Symbols as being more
akin to numbers than strings.

They’re numbers with a human face.

In some ways, a comparison with interned strings in Java can be helpful.

Hi –

On Sun, 1 Jan 2006, Malte M. wrote:

values…
It’s actually not that complex – basically:

a = “string” # a contains a reference to that string
a = 1 # a contains the actual (immediate) value 1

(“string” itself is a literal string.)

I think that Fixnums, Symbols, true, false, and nil are the only
objects that appear in variables as immediate values. Most of the
time you’re dealing with references.

In practical terms, it doesn’t normally matter too much. You always
use the same syntax for sending messages to objects through variables,
whether the variable contains a reference or an immediate value.

David


David A. Black
[email protected]

“Ruby for Rails”, from Manning Publications, coming April 2006!

On 1/1/06, [email protected] [email protected] wrote:

It’s actually not that complex – basically:

I decided to throw up some examples for those who like examples.

a = “string” # a contains a reference to that string

irb(main):007:0> a = “foo”
=> “foo”
irb(main):008:0> b = “foo”
=> “foo”
irb(main):009:0> c = “foo”
=> “foo”
irb(main):010:0> a.object_id
=> -605890256
irb(main):011:0> b.object_id
=> -605898166
irb(main):012:0> c.object_id
=> -605912906

a = 1 # a contains the actual (immediate) value 1

irb(main):001:0> a = 1
=> 1
irb(main):002:0> b = 1
=> 1
irb(main):003:0> c = 1
=> 1
irb(main):004:0> a.object_id
=> 3
irb(main):005:0> b.object_id
=> 3
irb(main):006:0> c.object_id
=> 3

(“string” itself is a literal string.)

But keep in mind, “string” is not an immediate value.

irb(main):013:0> “string”.object_id
=> -605956386
irb(main):014:0> “string”.object_id
=> -605962596

Symbols, OTOH, are: (Seeing as they’re just named numbers :))
irb(main):015:0> :my_sym.object_id
=> 4073742
irb(main):016:0> :my_sym.object_id
=> 4073742

Hope these examples help.