From: Ross Bamford Date: 2006-01-01T05:52:56+09:00 Subject: Re: What is the difference between :foo and "foo" ? On Sat, 31 Dec 2005 20:10:13 -0000, wrote: > I'm somewhat new to ruby, and for me it has made most sense to do an ri > on Symbol to check out the api. When I first ran across symbols in the > Pickaxe it didn't make much sense. I started out just taking it on > blind faith how they get used in different frameworks api's. Seems > like what symbols are is different from how they might be best used. > Here's something I wrote up to play around with how they work. I was > surprized to find out that any identifier (class, method, variable, > etc) in a ruby script gets turned into a symbol. > > Bill > > # Definition: A symbol is an object of class Symbol. The symbol > # has only one instance with given name that can be found by > # calling the method id2name or to_s. The symbol has a unique > # integer value that can be found by calling the to_i method. > # The Symbol class method all_symbols will give an array of all > # symbols. A symbol is automatically generated for any > # identifier used or seen within a ruby program including > # constants, classes, methods, variables, etc. Once a symbol is > # created it is never garbage collected. > S'funny, my RI says: +Symbol+ objects represent names and some strings inside the Ruby interpreter. They are generated using the +:name+ and +:"string"+ literals syntax, and by the various +to_sym+ methods. The same +Symbol+ object will be created for a given name or string for the duration of a program's execution, regardless of the context or meaning of that name. Thus if +Fred+ is a constant in one context, a method in another, and a class in a third, the +Symbol+ +:Fred+ will be the same object in all three contexts. But anyway, > def find_and_display_symbol(symbol_name) > found_symbol = Symbol.all_symbols.find { |s| s.to_s == symbol_name } > if (found_symbol != nil) > puts "symbol found with name: #{found_symbol.to_s} and " + > "id: #{found_symbol.to_i}\n\n" > else > puts "unable to find symbol #{symbol_name}\n\n" > end > end > > # create a symbol object called symbol_name > :my_new_symbol > > # display the newly created symbol by going through all symbols > # see find_and_display_symbol function below > find_and_display_symbol('my_new_symbol') > > # symbols apparently don't have to be a legal identifier > :"Hello World!" > find_and_display_symbol("Hello World!") > > #local variable creates a symbol > silly_local_variable = "Hello" > find_and_display_symbol('silly_local_variable') > > # Any referenced identifier will create a symbol, even if the symbol is > # undefined: > defined? undefined_variable > find_and_display_symbol('undefined_variable') > > # Symbols already exist for builtin classes and methods > find_and_display_symbol('String') > find_and_display_symbol('each') > find_and_display_symbol('$LOAD_PATH') > > # list all symbols > sym_array = Symbol.all_symbols > sym_array.sort! {|a,b| a.to_s <=> b.to_s } > sym_array.each {|s| puts "name: #{s.to_s} id: #{s.to_i}\n" } > Hey, that could be a good basis for a nuby file for my collection (http://roscopeco.co.uk/code/noob), maybe as part of an expanded version of the existing syms-methods thing (which isn't too hot anyway to be honest). Mind me doing that? Cheers, -- Ross Bamford - rosco@roscopeco.remove.co.uk