Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 114 lines (80 sloc) 3.331 kB
6b2795d @coreyhaines initial import
authored
1 = overloaded_methods
2
bc078d3 @coreyhaines filling out readme
authored
3 http://github.com/coreyhaines/overloaded_methods
6b2795d @coreyhaines initial import
authored
4
5 == DESCRIPTION:
6
586c1da @coreyhaines starting to fill out readme
authored
7 overloaded_methods was inspired by erlang pattern matching. Basically, the idea is that you can specify a method and several code blocks based on aspects of the parameters. Here's an example:
8
9 overload_method :even_odd do |m|
10 m.when {|number| number % 2 == 0 }.do { :even }
11 m.when {|number| number % 2 == 1 }.do { :odd }
12 end
13
14 You can then call even_odd(2) and get :even, even_odd(3) gives you :odd.
6b2795d @coreyhaines initial import
authored
15
25d94d9 @coreyhaines starting to fill out readme
authored
16 the specs have more examples.
17
61485ce @coreyhaines filling out readme
authored
18 Special thanks to Jim Weirich for suggesting some great improvements in the syntax.
19
d9c62b4 @coreyhaines filling out readme
authored
20 There is an alternate syntax with pattern/does
21 overload_method :even_odd do |m|
22 m.pattern {|number| number % 2 == 0 }.does { :even }
23 m.pattern {|number| number % 2 == 1 }.does { :odd }
24 end
25 However, this syntax may or may not be deprecated in favor of the when/do syntax.
26
52d9be2 @coreyhaines fibonacci example
authored
27 Here's an example of Fibonacci calculation:
28 class CalculatesFibonacci
29
30 class << self
31 include OverloadedMethods
32 overload_method :entry do |m|
33 m.when{|which| which <= 1}.do{|which| which}
34 m.default{|which|
35 CalculatesFibonacci.entry(which-1) + CalculatesFibonacci.entry(which-2)
36 }
37 end
38 end
39 end
40
41
42
6b2795d @coreyhaines initial import
authored
43 == FEATURES/PROBLEMS:
44
45 * FIX (list of features or problems)
46
47 == SYNOPSIS:
48
d9c62b4 @coreyhaines filling out readme
authored
49 The following is from the specs, just some examples of how to use it.
50
51 class WithWhenDo
52 include OverloadedMethods
53
54 overload_method :even_odd do |m|
55 m.when {|number| number % 2 == 0 }.do { :even }
56 m.when {|number| number % 2 == 1 }.do { :odd }
57 end
58
59 overload_method :hello_world do |m|
60 m.when { true }.do {'hello, world'}
61 end
62
63 overload_method :return_params do |m|
64 m.when { |param1, param2| true}.do {|param1, param2| [param1, param2]}
65 end
66
67 overload_method :two_params_collected do |m|
68 m.default { |param1, *params| [param1, params] }
69 end
70
71 overload_method :number_of_digits do |m|
72 m.when {|number| number < 10}.do {1}
73 m.when {|number| number< 100}.do {2}
74 m.default { 'more than two digits is crazy talk'}
75 end
76
77 overload_method :default_returns_parameters do |m|
78 m.default { |*params| params}
79 end
80
81 end
6b2795d @coreyhaines initial import
authored
82
83 == REQUIREMENTS:
84
e9436b8 @coreyhaines filling out readme
authored
85 Ruby
6b2795d @coreyhaines initial import
authored
86
87 == INSTALL:
88
586c1da @coreyhaines starting to fill out readme
authored
89 working on gemspec for github.
6b2795d @coreyhaines initial import
authored
90
91 == LICENSE:
92
93 (The MIT License)
94
586c1da @coreyhaines starting to fill out readme
authored
95 Copyright (c) 2008 Corey Haines
6b2795d @coreyhaines initial import
authored
96
97 Permission is hereby granted, free of charge, to any person obtaining
98 a copy of this software and associated documentation files (the
99 'Software'), to deal in the Software without restriction, including
100 without limitation the rights to use, copy, modify, merge, publish,
101 distribute, sublicense, and/or sell copies of the Software, and to
102 permit persons to whom the Software is furnished to do so, subject to
103 the following conditions:
104
105 The above copyright notice and this permission notice shall be
106 included in all copies or substantial portions of the Software.
107
108 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
109 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
110 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
111 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
112 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
113 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
114 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Something went wrong with that request. Please try again.