[#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:6170] Re: Mapping text to method calls

From: hipster <hipster@...4all.nl>
Date: 2000-11-08 15:51:44 UTC
List: ruby-talk #6170
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

In This Thread

Prev Next