From: hipster Date: 2000-11-09T00:51:44+09:00 Subject: [ruby-talk:6170] Re: Mapping text to method calls On Thu, 09 Nov 2000 00:40:03 +0900, aschneiderman@my-deja.com wrote: > 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 > > In Python, this is fairly easy to do. How do you do it in Ruby? e.g.: ---8<--- class Dispatcher def initialize @map = Hash.new end def dispatch id # or "eval id" (relies on method_missing) begin method(id).call rescue NameError puts "undefined method: #{id}" end end private def a puts "called a" end def b puts "called b" end def method_missing id puts "method_missing: #{id.id2name}" end end d = Dispatcher.new while (line = gets) != nil d.dispatch line.chomp! end ---8<--- $ dispatcher a called a foo undefined method: foo $ _ regards, Michel