Ruby Programming
Previous Page Home Next Page

Calling Methods Dynamically

C and Java programmers often find themselves writing some kind of dispatch table: functions which are invoked 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.


Oh, and by the way, it does much more than the C version---it's dynamic. The Ruby version 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") 13, '.') "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)
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 state explicitly 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 = amt
    $here = binding

a = eval "puts @amt", $here eval "puts @amt"

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.
Ruby Programming
Previous Page Home Next Page

Published under the terms of the Open Publication License Design by Interspire