Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
An implementation of multimethod dispatching in Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
README.textile
Rakefile
VERSION.yml
defmulti.gemspec

README.textile

defmulti

NOTE: This gem runs perfectly fine on Ruby 1.9.1.

This gem is an implementation of multi-method dispatching in Ruby. This allows you to define multiple blocks of code that can execute, depending on certain conditions:

 require "defmulti"
  
  class Factorial
    class << self
      defmulti :generate,
        lambda {|n| n == 1} => 1,
        nil                 => lambda {|n| n * generate(n - 1) }
    end
  end
  
  Factorial.generate 1
  #=> 1
  Factorial.generate 2
  #=> 2
  Factorial.generate 3
  #=> 6
  Factorial.generate 4
  #=> 24
  Factorial.generate 5
  #=> 120

This is a thought experiment. I was thinking about a language in which the if/then/else construct would not exist. How would such a language evolve? That is what I was trying to determine.

NOTE: This isn’t ready for production use. Neither is it used in production anywhere.

If you want to run the tests, you’ll need the Dfect library.

Something went wrong with that request. Please try again.