[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Stephen Cox

7/16/2007 9:53:00 AM

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

--
Posted via http://www.ruby-....

10 Answers

Matthew Rudy Jacobs

7/16/2007 10:12:00 AM

0

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


--
Posted via http://www.ruby-....

Stephen Cox

7/16/2007 10:22:00 AM

0

Matthew Rudy 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.

--
Posted via http://www.ruby-....

Matthew Rudy Jacobs

7/16/2007 10:24:00 AM

0

Stephen Cox wrote:
> Matthew Rudy 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"

:)

--
Posted via http://www.ruby-....

Stephen Cox

7/16/2007 10:27:00 AM

0

Ahhh.. I think I got it.

Thanks...

--
Posted via http://www.ruby-....

John Joyce

7/16/2007 12:25:00 PM

0

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 Joyce
On Jul 16, 2007, at 5:27 AM, Stephen Cox wrote:

> Ahhh.. I think I got it.
>
> Thanks...
>
> --
> Posted via http://www.ruby-....
>


Hal E. Fulton

7/18/2007 10:39:00 PM

0

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

7/19/2007 2:10:00 AM

0

Hal Fulton 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?


--
Posted via http://www.ruby-....

Gary Wright

7/19/2007 2:36:00 AM

0


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 Wright

Joel VanderWerf

7/19/2007 2:50:00 AM

0

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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

cypher.dp

7/21/2007 11:40:00 AM

0

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#e495f6...

Hope it helps

Dominik



2007/7/19, Joel VanderWerf <vjoel@path.berkeley.edu>:
> 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.)
>
> --
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
>
>