Multiple dispatch in Ruby

I’ve been reading recently about Common Lisp and CLOS and I’ve been intrigued by their radically different approach to OO and some of their interesting features, one of which is multiple dispatch. Most mainstream OO languages don’t support multiple dispatch natively although one can sometimes pull it off with tricks (e.g..: for C++, check out More Effective C++ from Scott Meyers or Alexandrescu’s Modern C++ Design or this article from Dr. Carlo Pescio in C++ Report).

Ruby doesn’t support multiple dispatch natively, but there’s an add-on called the Multiple Dispatch Library (multi) that let’s you do it and it’s a snap to use. Even a Rube n00b like me had it going in 5 minutes…

Installing is easy with gem:

$ sudo gem install multi
Attempting local installation of 'multi'
Local gem file not found: multi*.gem
Attempting remote installation of 'multi'
Updating Gem source index for: http://gems.rubyforge.org
Successfully installed multi-0.1

and here’s a sample program that shows how it works.

require 'rubygems'                                                                                                             
require_gem 'multi'                                                                                                            
                                                                                                                               
class Asteroid                                                                                                                 
end                                                                                                                            
                                                                                                                               
class Spaceship                                                                                                                
end                                                                                                                            
                                                                                                                               
multi(:collide, Asteroid, Asteroid)    { print "BAM! Two asteroids collide.\n" }                                               
multi(:collide, Asteroid, Spaceship)   { print "BAM! Asteroid, Spaceship\n" }                                                  
multi(:collide, Spaceship, Spaceship)  { print "BAM! Spaceship, Spaceship\n" }                                                 
multi(:collide, Spaceship, Asteroid)   { print "BAM! Spaceship, Asteroid\n" }

collide(Asteroid.new, Asteroid.new)                                                                                            
collide(Asteroid.new, Spaceship.new)                                                                                           
collide(Spaceship.new, Asteroid.new)                                                                                           
collide(Spaceship.new, Spaceship.new)        

Voila. Multiple dispatch.

Programming Ruby: The Pragmatic Programmers\' Guide, Second Edition
Practical Common Lisp
More Effective C++: 35 New Ways to Improve Your Programs and Designs
Modern C++ Design: Generic Programming and Design Patterns Applied

Reddit

One thought on “Multiple dispatch in Ruby

Leave a Reply

Your email address will not be published. Required fields are marked *