From: Chris Irish Date: 2005-12-03T06:05:45+09:00 Subject: Re: Make a symbol represent a method? Jacob Fugal wrote: >On 12/2/05, Chris Irish wrote: > > >>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? >> >> > >Sure thing. The symbol is not a reference to the method at all. What >the symbol is is a *key* into the objects method table, the value >corresponding to that key is the reference to the Method object. I can >use a symbol (it'll "auto-vivify", for lack of a better word) even >without a corresponding method. > >As far as syntax for calling a method just using it's named symbol, >there are two ways you can do this. The first is to use send: > > class Greeter > def hello( name ) > puts "Hello, #{name}!" > end > end > > greeter = Greeter.new > greeter.send( :hello, "World" ) > # prints "Hello, World!" > >An alternate technique is to grab an actual reference to the method >itself. This is available using the 'method' method: > > greeter = Greeter.new > meth = greeter.method( :hello ) > meth.call( "World" ) > # same output > >This latter approach allows you to perform the name lookup only once, >even if you need to call the method several times. It's also useful >when metaprogramming; you can grab a reference to an existing method >before redefining it, then call the original method from inside the >redefined method. Nifty! > >Jacob Fugal > > > Thank's everyone :)