From: Chris Irish Date: 2005-12-03T05:38:41+09:00 Subject: Re: Make a symbol represent a method? Jacob Fugal wrote: >On 12/2/05, Chris Irish wrote: > > >>I just read a FAQ page online about Ruby symbols and someone mentions this >> >>Also, the entirely realistic reasoning for using symbols: If you >>are going to refer to a method name, use a symbol. Because by >>defining the method, the symbol exists anyway. >> >>Can someone show me an example of this? Is this referring to the method >>of a class? >> >> > >It simply comes down to this. When you define a method the method >definition is saved away and connected to a lookup table for the >object you defined the message on. The key into that lookup table? A >symbol representing the method's name. So if I define: > > def hello > puts "Hello, world!" > end > >The body is complied into a Method object, and that Method object is >then attached to the current self (Kernel, is you're not in anything >else) with the symbol :hello as the key. After defining the above, try >this: > > send(:hello) > >Anytime you make a method call, it's essentially just an invocation of >send with the method name and arguments as paramenters. (Well, it's a >little more complicated, but that's why I said "essentially".) > >Anyways, the point of the quote from the FAQ is that just be defining >the method hello, the symbol :hello already exists, since it's used to >index the receiver's method table. So, if you're going to be passing >the method name around (common with metaprogramming), you save by >using the already existing :hello symbol, instead of creating a new >"hello" String object. > >I hope that makes sense... > >Jacob Fugal > > > K.... I get it One more thing, Can I use the symbol to call a method and pass arguments to it? Or is it just a reference to the methods name so that's not possible?