Symbols

I’m an xbase (dbase/FoxBase & Pro/Clipper) and cobol programmer from way
back. (Been writing code since 1980) Believe it or not, I make a VERY
nice living maintaining legacy code. Anyway, decided to learn a new
modern language. Hence Ruby. I wanted something that was completly OOP.
Anyway, been playing with it for a while. And having trouble getting
what Symbols are used for?

I know what they are (I’ve read the manuals). But what are the practical
uses for them? I mean, couldn’t I just use a constant or a global
variable? I know symbols keep their name unique (even if used to id a
class or variable). But is that the only benefit?

Thanks…

symbols are just strings,
except they can’t be changed,
they’re like the Fixnum of the string world.

so they aren’t variables or constants in themselves,
but they are very useful as switches for case statements and such

how about this example;

we want to define the types of messages we can send,
so we declare a constant

MESSAGE_TYPES = {
1 => “email”,
2 => “sms”,

}

then if we want to run a type_dependent function we’d do

def do_stuff(message_type)
case message_type
when 1
do email stuff
when 2
do sms stuff
end
puts “done stuff for #{MESSAGE_TYPES[message_type]}”
end

which works, but we’ve made it less understandablel

instead we can do

def do_stuff(message_type)
case message_type
when :email
do email stuff
when :sms
do sms stuff
end
puts “done stuff for #{message_type}”
end

sweet!!!

Stephen Cox wrote:

I’m an xbase (dbase/FoxBase & Pro/Clipper) and cobol programmer from way
back. (Been writing code since 1980) Believe it or not, I make a VERY
nice living maintaining legacy code. Anyway, decided to learn a new
modern language. Hence Ruby. I wanted something that was completly OOP.
Anyway, been playing with it for a while. And having trouble getting
what Symbols are used for?

I know what they are (I’ve read the manuals). But what are the practical
uses for them? I mean, couldn’t I just use a constant or a global
variable? I know symbols keep their name unique (even if used to id a
class or variable). But is that the only benefit?

Thanks…

Matthew R. wrote:

def do_stuff(message_type)
case message_type
when :email
do email stuff
when :sms
do sms stuff
end
puts “done stuff for #{message_type}”
end

Thanks.

but doing something like:

email = 1
sms = 2

And then the same case statement.

But instead of testing symbols you’d test variables. Same thing right?
Except symbols can be used anywhere in the application? And may seem a
more elegant approach to strings that everything needs access too.

Stephen Cox wrote:

Matthew R. wrote:

def do_stuff(message_type)
case message_type
when :email
do email stuff
when :sms
do sms stuff
end
puts “done stuff for #{message_type}”
end

Thanks.

but doing something like:

email = 1
sms = 2

And then the same case statement.

But instead of testing symbols you’d test variables. Same thing right?
Except symbols can be used anywhere in the application? And may seem a
more elegant approach to strings that everything needs access too.

but with “email = 1” you don’t get any reversibility.

when somethings broken,
you’re in a breakpoint;

“oh, i wonder what the message type is”

message_type
-> 1

“wow, that’s useful”

:slight_smile:

Ahhh… I think I got it.

Thanks…

Symbols also are a lot slimmer objects.
They’re very popular for accessing things because calling a symbol is
noticeably less overhead than other object types.

Rails makes heavy use of hashes of symbols.

John J.

On Jul 18, 2007, at 10:09 PM, Stephen Cox wrote:

yeah I figured out symbols were the actual value. But one question,
if a
symbol is no longer used what happens to it? Is it released when the
procedure it was create in completes?

MRI never garbage collects symbols. If you create symbols based on
external data you have to be careful.

I don’t know how JRuby or Rubinious handles symbols.

Gary W.

Stephen Cox wrote:

Ahhh… I think I got it.

Thanks…

Of course, note that you can go back and forth between
symbols and strings, but not between variables and strings.

email = 1 # It’s just a 1… doesn’t know its own name.

:email.to_s # “email”
“email”.to_sym # :email

Hal

Stephen Cox wrote:

yeah I figured out symbols were the actual value. But one question, if a
symbol is no longer used what happens to it? Is it released when the
procedure it was create in completes?

No. Symbols accumulate forever. They cannot be garbage collected in
current ruby. A variable whose value is a symbol actually contains a
number, which is an index into a table, rather than the characters of
the symbol. Ruby has to ensure that, as long as the program runs, this
table entry contains the same string:

irb(main):003:0> :a.to_i
=> 14033
irb(main):004:0> 14033.to_sym
=> :a

This is another reason to use strings for most purposes. (Also, strings
look better in YAML, especially as hash keys.)

Hal F. wrote:

Stephen Cox wrote:

Ahhh… I think I got it.

Thanks…

Of course, note that you can go back and forth between
symbols and strings, but not between variables and strings.

email = 1 # It’s just a 1… doesn’t know its own name.

:email.to_s # “email”
“email”.to_sym # :email

Hal

yeah I figured out symbols were the actual value. But one question, if a
symbol is no longer used what happens to it? Is it released when the
procedure it was create in completes?

You’re not the first one who’s asking…
http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/7e9a4d1cb55d1c1f/e495f6051aebe0ae?lnk=gst&q=symbols+strings&rnum=3#e495f6051aebe0ae

Hope it helps

Dominik

2007/7/19, Joel VanderWerf [email protected]: