Classic Duck Typing Example

CLassic Duck Typing Example

 1
 2class Duck
 3  def talk
 4    'Quack!'
 5  end
 6
 7  def swim
 8    'Paddle paddle paddle...'
 9  end
10end
11
12class Goose
13  def talk
14    'Honk!'
15  end
16  def swim
17    'Splash splash splash...'
18  end
19end
20
21class Student
22  def talk
23    "Hello!"
24  end
25
26  def walk
27    'Step, Step, Step'
28  end
29
30  def method_missing (arg)
31    puts "forgettabouttit: I dont know how to #{arg}"
32  end
33
34end
35
36
37things = [Student.new, Goose.new, Duck.new]
38
39def make_it_talk(x)
40  x.talk
41  x.swim
42end
43
44things.each { |t| make_it_talk(t) }
45