Forum: Ruby Symbols

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4ec688bc969d57547ff77e873600d663?d=identicon&s=25 Stephen Cox (crag)
on 2007-07-16 11:51
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...
E60b2dc57668b5662ce3f07781e41710?d=identicon&s=25 Matthew Rudy Jacobs (matthewrudy)
on 2007-07-16 12:12
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...
4ec688bc969d57547ff77e873600d663?d=identicon&s=25 Stephen Cox (crag)
on 2007-07-16 12:20
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.
E60b2dc57668b5662ce3f07781e41710?d=identicon&s=25 Matthew Rudy Jacobs (matthewrudy)
on 2007-07-16 12:23
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"

:)
4ec688bc969d57547ff77e873600d663?d=identicon&s=25 Stephen Cox (crag)
on 2007-07-16 12:27
Ahhh.. I think I got it.

Thanks...
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-07-16 14:27
(Received via mailing list)
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
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 Hal Fulton (Guest)
on 2007-07-19 00:40
(Received via mailing list)
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
4ec688bc969d57547ff77e873600d663?d=identicon&s=25 Stephen Cox (crag)
on 2007-07-19 04:09
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?
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2007-07-19 04:38
(Received via mailing list)
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2007-07-19 04:50
(Received via mailing list)
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.)
1d62a7d62093aa945e67de4119e81dfd?d=identicon&s=25 unknown (Guest)
on 2007-07-21 13:40
(Received via mailing list)
You're not the first one who's asking...
http://groups.google.com/group/comp.lang.ruby/brow...

Hope it helps

Dominik



2007/7/19, Joel VanderWerf <vjoel@path.berkeley.edu>:
This topic is locked and can not be replied to.