From: Peter Cooper Date: 2007-06-25T08:56:51+09:00 Subject: Re: variables to call methods in ruby... On 6/25/07, yermej@gmail.com wrote: > On Jun 24, 5:48 pm, Tim Hunter wrote: > > Harnish wrote: > > > Essentially I want to call Test.func; but "func" lies in the variable > > > testfunc; is it possible for me to invoke Test.func using variable > > > testfunc? If yes, how? Any ideas, greatly appreciated. > > > > Check out the "__send__" method: > > > > var = "func" > > obj.__send__(var, args) > > I've always used it without the underscores: You can, but in case you're wondering the whys and wherefores of all this.. it's possible to easily change the "send" method on a class with anyone noticing.. this could cause "Bad Things"(tm) to happen. It's possible to redefine __send__ too, but a warning is given and it's generally considered bad form to redefine, delete, or otherwise modify methods using the __x__ naming convention. You'll notice classes like BlankSlate ( http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc ) take this into account: class BlankSlate instance_methods.each { |m| undef_method m unless m =~ /^__/ } end Cheers, Peter Cooper http://www.rubyinside.com/