From: Kent Dahl Date: 2002-12-10T06:19:29+09:00 Subject: Re: Newbie: invoking class methods James Davis wrote: > > (Using Ruby 1.6.6) > I am writing a game. A "splash" screen displays four buttons to the > user. Each button has an action assigned to it, loaded from a file. > When a user clicks a button, I want to execute a class method such as > Game.new. First, I thought I wanted a Proc for this purpose, so I > tried: > ba = proc {ba} # ba = "Game.new" > ... > ba.call > This approach didn't work. Didn't produce errors. Note that you reassign ba in the same context as the proc is defined, which means ba will point to the proc itsef. So when you call the proc, it just returns itself. > So I switched to > eval(ba) > which does work, but I was hoping for a better technique. Eval seems > crude and unsafe to me, particularly since the button actions are > loaded from a file. I am wondering if there might be another approach > such as something like "instance_method" for class methods, or send, > or yield. If they all are Game class objects, then Game.send(method_name) should suffice. You could also use Game.method(method_name) to get similar to instance_method, however it won't be an UnboundedMethod (since you've bound it to the class Game object) HTH -- (\[ Kent Dahl ]/)_ _~_ __[ http://www.stud.ntnu.no/~kentda/ ]___/~ ))\_student_/(( \__d L b__/ NTNU - graduate engineering - 5. year ) ( \__\_�|�_/__/ ) _)Industrial economics and technological management( \____/_�_\____/ (____engineering.discipline_=_Computer::Technology___)