forked from RubyoffRails/Episode1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzoo.rb
86 lines (61 loc) · 838 Bytes
/
zoo.rb
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#Zoo
module Animal
def eat(food)
@meals ||= 0
if likes?(food)
@meals += 1
true
else
false
end
end
def likes?(food)
acceptable_food.include?(food)
end
def acceptable_food
[]
end
def full?
false
end
end
class Panda
include Animal
def acceptable_food
[Bamboo.new]
end
def full?
@meals > 30
end
end
class Lion
include Animal
def acceptable_food
[Wildebeests.new, Zeebras.new]
end
def full?
@meals > 10
end
end
class Food
def ==(other)
other.is_a? self.class
end
end
class Tacos < Food; end
class Wildebeests < Food; end
class Zeebras < Food; end
class Bamboo < Food; end
class Zookeeper
def feed(args={})
food = args.fetch(:food)
animal = args.fetch(:to)
animal.eat(food)
end
end
class Food
attr_reader :name
def initialize(name)
@name = name
end
end