-
Notifications
You must be signed in to change notification settings - Fork 0
/
duck.rb
87 lines (72 loc) · 1.24 KB
/
duck.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
87
class FlyBehaviour
def initalize(duck)
@duck = duck
end
def fly
if duck.class == DecoyDuck || RubberDuck
puts "I dont fly"
else
puts("flying duck")
end
end
end
class RocketPowerdFlyBehaviour
def fly
puts("using rocket powered flight")
end
end
class QuackBehaviour
def initialize(duck)
@duck = duck
end
def quack
if duck.class == RubberDuck
puts "squeek"
elsif duck.class == DecoyDuck
puts "I am doing nothing"
else
puts("quack")
end
end
end
class SwimBehaviour
def initialize(duck)
@duck = duck
end
def swim
if duck.class == DecoyDuck
puts("sink")
else
puts("swim")
end
end
end
class Duck
attr_reader :swim_behaviour, :quack_behaviour
attr_accessor :fly_behaviour
def initialize
@swim_behaviour = SwimBehaviour.new(self).swim
@fly_behaviour = FlyBehaviour.new(self)
@quack_behaviour = QuackBehaviour.new(self)
end
def fly
fly_behaviour.fly
end
def display
puts self.class
end
def quack
quack_behaviour.quack
end
def swim
swim_behaviour.swim
end
end
class RedheadDuck < Duck
end
class MallerdDuck < Duck
end
class RubberDuck < Duck
end
class DecoyDuck < Duck
end