From: Logan Capaldo Date: 2006-04-06T02:31:15+09:00 Subject: Re: what are sympbols?!? On Apr 5, 2006, at 1:14 PM, Alain wrote: > Hi, simple question here but I cant figure it out after reading the > Ruby > book. > > How symbols work? I come from a Java background and I cant understand > what symbols are and how to work with them. I cant figure out when > I am > supposed to use them in a function. Seems to me that the description > from the book is too short for such a concept. > > Please, explain! > > Thanks! > > -- > Posted via http://www.ruby-forum.com/. > Well you can search the archives, this question has been asked many times. But I can take a stab at it, I like to think of symbols as names. Names that can be used in various places, for instance, using the send method, it wants to know the name of the method to send, so you use a symbol "Hello".send(:display) Or if you are using a hash, you can use symbols as keys to name the elements: { :height => 27, :width => 34, :color => "green" } There is only one symbol for every name, a symbol :x is not the object named x, it is the name, "x". :Logan is not Logan (me), it's the name "Logan" Many people can have the same name but different objects. Two people can be called Joe, but they still have the same name "Joe". Likewise in ruby: { :class => "A" } 1.send(:class) The hash key has the same name as the method being sent via send, but the results are different. A symbol doesn't point, it is just the name. I point because one of the advantages of symbols is that they are fast: :name == :name is just as quick as comparing a number. People use this detail all the time in their code. They need to give something a name, a name that the user will probably never see (like a key in a hash, or the argument to a method, like say draw_at(0,0, :circle). ) The name is easier to remember than any other arbitrary object (often times a number will be used or an enum in other languages). So in short IMO, a symbol is a name.