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-forum.com/.