[#6363] Re: rescue clause affecting IO loop behavior — ts <decoux@...>

>>>>> "D" == David Alan Black <dblack@candle.superlink.net> writes:

17 messages 2000/11/14
[#6367] Re: rescue clause affecting IO loop behavior — David Alan Black <dblack@...> 2000/11/14

Hello again --

[#6582] best way to interleaf arrays? — David Alan Black <dblack@...>

Hello --

15 messages 2000/11/26

[#6646] RE: Array Intersect (&) question — Aleksi Niemel<aleksi.niemela@...>

Ross asked something about widely known and largely ignored language (on

23 messages 2000/11/29
[#6652] RE: Array Intersect (&) question — rpmohn@... (Ross Mohn) 2000/11/29

aleksi.niemela@cinnober.com (Aleksi Niemel) wrote in

[#6723] Re: Array Intersect (&) question — Mathieu Bouchard <matju@...> 2000/12/01

> >Use a hash. Here's code to do both and more. It assumes that

[#6656] printing/accessing arrays and hashes — raja@... (Raja S.)

I'm coming to Ruby with a Python & Common Lisp background.

24 messages 2000/11/30

[ruby-talk:6172] RE: Mapping text to method calls

From: Aleksi Niemel<aleksi.niemela@...>
Date: 2000-11-08 16:10:36 UTC
List: ruby-talk #6172
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

In This Thread

Prev Next