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

C

Chad Perrin

I'm communicating. You're just not listening. Effective communication
requires a willing listener, and you seem to be uninterested in such.

My point exactly: I used sloppy phrasing to try to get a point across,
made sure I identified it as sloppy phrasing, and you've done everything
in your power to nitpick my phrasing rather than grok my meaning.
 
G

Gregory Brown

Please end this thread, for the sake of all things good and decent.

Agreed. I apologize for throwing fuel on the fire accidentally.

If nothing else, the difference between "foo" and :foo has been answered.

Let's all be friends ;)
 
G

Gregory Brown

Funny, I don't recall receiving said off-list emails.

Look back through this thread, a few of my messages were sent just to
you and not to the list. The post to Austin was also meant to be
private. Okay, I'm done with this thread now :)

Happy new year, all
 
M

Malte Milatz

Christian Neukirchen:
Malte Milatz:
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
 
B

Bill.Dolinar

Steve said:
Long time Rubyists, people with heavy CS studies, and people who have
experience in languages with symbols (LISP, I guess) tend to talk more about
how it's implemented, while programmers (probably without CS degrees)
migrating from languages not containing symbols are more interested in what
it does than how it's implemented.

I asked if it's an object with 2 attributes, an integer key and a string
value. Someone says no, it's an object with one attribute that can be seen 2
ways, a string or an integer. From my point of view, he's saying the same
thing. I was describing what it can do for me, he was describing something
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" }
 
R

Ross Bamford

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.

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,
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" }

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,
 
J

Johannes Friestad

My understanding of things so far was that :sym and 3 are immediate
I wonder that too. What about:
a, b =3D :sym, :sym
I guess a and b hold separate references to the same object, much as if I
did
a, b =3D 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=3DObject.new
or
a=3D"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=3D4
'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
 
H

Hal Fulton

James said:
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
 
G

Gregory Brown

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.

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

szpak

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).
 
J

John W. Kennedy

James said:
Austin Ziegler 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.

--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"
 
D

dblack

Hi --

Christian Neukirchen:

I'm happy I'm not forced to dig into the details of Ruby's immediate
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
(e-mail address removed)

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top