Ruby は「包含ポリモーフィズム」をサポートしていますか? これはダックタイピングと同じですか?
そうでない場合、Ruby におけるポリモーフィズムとダックタイピングの違いは何ですか?
誰かが私の例を以下に説明してください:
# superclass - inheritance
class Animal
def make_noise
# define abstarct method (force override)
raise NoMethodError, "default make_noise method called"
end
end
# module - composition
module Fur
def keep_warm
# etc
end
end
# subclass = is-a relationship
class Bird < Animal
# override method - polymorphism
def make_noise
"Kaaw"
end
end
class Cat < Animal
# module mixin = has-a relationship
include Fur
def make_noise
"Meow"
end
end
class Radio
# duck typing (no inheritance involved)
def make_noise
"This is the news"
end
end
class Coat
include Fur
end
animals = [Bird,Cat,Radio,Coat,Animal]
animals.each do |a|
# polymorphism or duck typing?
obj = a.new
if obj.respond_to? 'make_noise'
puts obj.make_noise
end
end