From: "Aleksi Niemelä" Date: 2000-11-09T01:10:36+09:00 Subject: [ruby-talk:6172] RE: Mapping text to method calls Besides hipster's beautiful dispatcher (I guess it needs argument passing, and that's that), I propose my version which gives a little bit more control. > I'm writing a voice-driven code editor, and I need to be able to map > command names to method calls. In other words, > "find save" --> editor.find("save") > "mail" --> mail.checkMail I'm not completely sure what you want to do here. Take an arbitrary string (coming from voice-recognizer) and if you match it with some predefined string (like find) then took action accordingly? How about using regexps and procs (not tested, just from the top of my head)? class Commander def initialize(editor, mailer) @editor, @mailer = editor, mailer @commands = { /find (.+)/ => proc {|match| self.find(match)}, /mail/ => proc { self.check_mail }, } end def process_command(string) @commands.keys.each do |regexp| if m = regexp.match(string) @commands[regexp].call(m) end end end def find(match) if match[1] @editor.find(match[1]) else puts "nothing to find specified) end end def check_mail @mailer.check_mail end end If you want direct handle to method, you might like to use Method instead of Proc. http://www.rubycentral.com/ref/ref_c_method.html Proc has the advantage (and disadvantage) of binding. > P.S. In theory, of course, I could just use emacs & some of the voice > tools that are out there. But hacking this together is a > whole lot more fun. Sounds fascinating, wanna tell more? - Aleksi