| Path: | lib/multimethod.rb |
| Last Update: | Fri Nov 24 21:01:32 GMT-5:00 2006 |
The Multimethod package implements dispatch of messages to multiple methods based on argument types.
Variadic methods and default values are supported.
Methods can be added and removed at run-time.
require 'multimethod'
class A
multimethod %q{
def foo(x) # matches any argument type
"#{x.inspect}"
end
}
multimethod %q{
def foo(Fixnum x) # matches any Fixnum
"Fixnum #{x.inspect}"
end
}
multimethod %q{
def foo(Numeric x) # matches any Numeric
"Numeric #{x.inspect}"
end
}
end
a = A.new
puts a.foo(:symbol) # ==> ":symbol"
puts a.foo(45) # ==> "Fixnum 45"
puts a.foo(12.34) # ==> "Numeric 12.34"
This library is not yet thread-safe, due to caching mechanisms used to increase performance. This will be fixed in a future release.
Multimethod was developed by:
Maybe you?