From: Brian Candler Date: 2010-06-21T21:11:20+09:00 Subject: Re: Ruby Basic Jes炭s Gabriel y Gal叩n wrote: >> Solution: >> >> eval("one") > > I'd rather use send: > > irb(main):001:0> def one > irb(main):002:1> "hi" > irb(main):003:1> end > => nil > irb(main):004:0> list = %w{one} > => ["one"] > irb(main):005:0> send(list.first) > => "hi" There are some other options too. (1) a (bound) Method object, which includes both the receiver and the method. class Person def initialize(name) @name = name end def greet puts "hi I'm #{@name}" end end fred = Person.new("Fred") m = fred.method(:greet) m.call # compare Jes炭s' suggestion: fred.send(:greet) (2) use a lambda instead of a method m = lambda { puts "Hello" } m.call # This also has access to surrounding local variables (closure) c = 0 m = lambda { c += 1; puts "Call number #{c}" } m.call m.call m.call -- Posted via http://www.ruby-forum.com/.