From: Bill Kelly Date: 2002-12-01T06:47:06+09:00 Subject: Re: Ruby Document Hi Shannon, > On Sun, 1 Dec 2002 04:05:36 +0900 > "Bill Kelly" wrote: > > > irb> "some_symbol".intern > > :some_symbol > This is what I don't understand. "some_symobol" is a instant value, > without a name. why the system assign a symbole :some_symbol to it? > So if I have a string "This is a test" then the intern will be > :This_is_a_test? That's sort of arbitrary... irb> "This is a test".intern :This is a test It's not arbitrary, it's just that, using String#intern, we can create symbols that we can't represent in the source code using the :name syntax. (Even though inspect will print them out that way.) > Relating to your explaination, and previous messages, I still have some vital > question about understanding symbol: > > 1. I tested using IRB, it seems that a symbol is not related to the name. > For example, > > irb(main):001:0> p :a.id > 3602702 > nil > irb(main):002:0> a=3 > 3 > irb(main):003:0> p a.id > 7 > nil > irb(main):004:0> p :a.id > 3602702 > nil > irb(main):005:0> > > It seems that I can create a symbol at any time, and it is not related > to the name. For example, after a=3, :a still point to the same place. > It seems that I have no way to use :a to reference to a? I am really > confused... :(( The trouble here would seem to be a confusion of the functionality of the Symbol class, with actual symbols in the source code. They aren't related that way.. All the Symbol class provides is essentially similar to: irb> class SymbolLike irb> @@syms = {} irb> def initialize(str) irb> @name = str irb> end irb> def to_s irb> @name irb> end irb> def SymbolLike.lookup(str) irb> @@syms[str] ||= SymbolLike.new(str) irb> end irb> end nil irb> a = SymbolLike.lookup("sym1") # irb> b = SymbolLike.lookup("sym1") # irb> a.id 22358968 irb> b.id 22358968 irb> a.to_s "sym1" ...Just think of it as an invisible hash table behind the scenes that ensures a single object instance of Symbol maps to any input string of identical characters. It hasn't any direct relation (that I'm aware of) to variable names or any other identifiers in the source code, except that the :name syntax is a short cut for "name".intern. > 2. I guessed some usage of symbol in your explanation. Please have a > look at the following javascript code: > > > > > > > > In the first alert, it will give out the name of the var you choose, but > the second will give out the value of the var you choose. I vaguely > guessed the use of symbol is related to this purpose. Can you implement > the above JavaScript code in Ruby? Is it related to the use of symbol? Well....... In Ruby I doubt whether I would see a use for symbols in this example... A variation might be: irb> class Test irb> def a; "3"; end irb> def b; "6"; end irb> end nil irb> t = Test.new # irb> prompt = "'which method do you want?'", "t.send(:a)" ["'which method do you want?'", "t.send(:a)"] irb> puts prompt.join(" ") 'which method do you want?' t.send(:a) nil irb> puts prompt.map{|s| eval s}.join(" ") which method do you want? 3 nil More simply, without the eval, we could say: irb> method_you_want = :a :a irb> puts "which method do you want? " + t.send(method_you_want) which method do you want? 3 nil Hope this helps, Bill