RoRLearn.com Home News Articles Tutorials Resources Books Forums
 

Home>Articles>

Calling Methods Dynamically

C and Java programmers often find themselves writing some kind of dispatch table: do specific processing based on a command. Think of a typical C idiom where you have to translate a string to a function pointer:

  typedef struct {
    char *name;
    void (*fptr)();
  } Tuple;

  Tuple list[]= {
    { "play",   fptr_play },
    { "stop",   fptr_stop },
    { "record", fptr_record },
    { 0, 0 },
  };

  ...

  void dispatch(char *cmd) {
    int i = 0;
    for (; list[i].name; i++) {
      if (strncmp(list[i].name,cmd,strlen(cmd)) == 0) {
        list[i].fptr();
        return;
      }
    }
    /* not found */
  }

In Ruby, you can do all this in one line. Stick all your command functions into a class, create an instance of that class (we called it commands), and ask that object to execute a method called the same name as the command string.

  commands.send(commandString)

Oh, and by the way, it does much more than the C version---it's dynamic. It will find new methods added at runtime just as easily. You don't have to write special command classes for send: it works on any object:

  "John Coltrane".send(:length)           # -> 13
  "Miles Davis".send("sub", /iles/, '.')  # -> "M. Davis"

Another way of invoking methods dynamically uses Method objects. A Method object is like a Proc object: it represents a chunk of code and a context in which it executes. In this case, the code is the body of the method, and the context is the object that created the method. Once we have our Method object, we can execute it sometime later by sending it the message call.

  trane = "John Coltrane".method(:length)
  miles = "Miles Davis".method("sub")

  trane.call               # -> 13
  miles.call(/iles/, '.')  # -> "M. Davis"

You can pass the Method object around as you would any other object, and when you invoke Method#call, the method is run just as if you had invoked it on the original object. It's like having a C-style function pointer, but in a fully-object oriented style. You can also use method objects with iterators.

  def double(a)
    2*a
  end

  mObj = method(:double)

  [1, 3, 5, 7].collect(&mObj)  # -> [2, 6, 10, 14]

As good things come in threes, here's yet another way to invoke methods dynamically. The eval method (and its variations such as class_eval, module_eval and \ instance_eval) will parse and execute an arbitrary string of legal Ruby source code.

  trane = %q{"John Coltrane".length}
  miles = %q{"Miles Davis".sub(/iles/, '.')}

  eval trane     # -> 13
  eval miles     # -> "M. Davis"

When using eval, it can be helpful to explicitly state the context in which the expression should be evaluated, rather than using the current context. You can obtain a context by calling Kernel#binding at the desired point.

  class CoinSlot
    def initialize(amt=Cents.new(25))
      @amt = amt
      $here = binding
    end
  end

  a = CoinSlot.new
  eval "puts @amt", $here
  eval "puts @amt"

Produces:

$0.25USD
nil

The first eval evaluates @amt in the context of the instance of class CoinSlot. The second eval evaluates @amt in the context of Object, where the instance variable @amt is not defined.

Performance Considerations

As we've seen in this section, there are several ways to invoke an arbitrary method of some object: Object#send, Method#call, and using the various flavors of eval. You may prefer to use any one of these techniques depending on your needs, but be aware that eval is significantly slower than the others (or for optimistic readers, ``send and call are significantly faster than eval'').

  require "benchmark"   # from the Ruby Application Archive
  include Benchmark

  test = "Stormy Weather"
  m = test.method(:length)
  n = 100000

  bm(12) {|x|
    x.report("call") { n.times { m.call } }
    x.report("send") { n.times { test.send(:length) } }
    x.report("eval") { n.times { eval "test.length" } }
  }

Produces:

                  user     system      total        real
call          0.250000   0.000000   0.250000 (  0.221879)
send          0.250000   0.000000   0.250000 (  0.227059)
eval          2.730000   0.070000   2.800000 (  2.650096)






 
Site Copyright © 2006-2007 RoRLearn.com All rights reserved. Privacy Policy | About Us | Contact Us | Site Map