GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Description: Implements pattern-based method dispatch for Ruby, inspired by Topher Cyll's multi.
Clone URL: git://github.com/dyoder/functor.git
functor / test.rb
100644 24 lines (20 sloc) 0.574 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
load 'lib/functor.rb'
class A
  include Functor::Method
  functor( :foo, Integer ) { |x| [ A, Integer ] }
  functor( :foo, String ) { |s| [ A, String ] }
  functor( :foo, Float ) { |h| [ A, Float ] }
end
 
class B < A
  functor( :foo, String ) { |s| [ B, String ] }
  functor( :foo, Float ) { |f| [ B, *A.functors[:foo].apply( self, f ) ] }
end
 
a = A.new ; b = B.new
puts a.foo( 7 ).inspect
puts b.foo( 'tentacles' ).inspect
 
fib ||= Functor.new do
  given( 0 ) { 0 }
  given( 1 ) { 1 }
  given( Integer ) { | n | self.call( n - 1 ) + self.call( n - 2 ) }
end
 
puts fib[ 7 ]