-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatch_table_example.rb
More file actions
56 lines (41 loc) · 1.24 KB
/
dispatch_table_example.rb
File metadata and controls
56 lines (41 loc) · 1.24 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module FoodPreparer
#this can be used for an after_initialize in rails so calling cook_meal
#uses the @lookup varibale, otherwise you could just substitute the dis-
#patch_table method in its place
def build_dispatch_table
@lookup = {
turkey: -> (n) { cook_turkey n },
ham: -> (n) { cook_ham n },
potroast: -> (n) { cook_potroast n }
}
end
def cook_meal
@lookup[meal].call years_of_experience
end
private
def cook_turkey n
puts "The Turkey was a #{n}/10"
end
def cook_ham n
puts "You cooked #{n} Hams"
end
def cook_potroast n
puts "the potroast took #{n} hours to cook"
end
#The above methods are trivial examples, but the key point is that the outputs
#of the methods should all return the same type of object, while using differnt
#logic to get there. They also should take the same # of arguments
#For instance, if #cook_turkey takes *n, then the others should also take *n
end
class Cook
include FoodPreparer
attr_reader :years_of_experience, :meal
@@possible_years_of_experience = *(1..10)
def initialize meal
@meal = meal
@years_of_experience = @@possible_years_of_experience.sample
build_dispatch_table
end
end
c = Cook.new(:turkey)
c.cook_meal