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…
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
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’)
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?
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.
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.
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?
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).
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.