D
Daniel Schierbeck
Tell me if this is stupid. I'm sure there's a simpler and faster way to
do this, but I'm interested in knowing whether the idea itself is...
well, stupid.
class Object
def quacks_like? (klass)
klass.public_instance_methods.each do |method|
return false unless respond_to? method
end
return true
end
end
class A
def foo; end
def bar; end
end
class B
def foo; end
end
if A.new.quacks_like? B
puts "A quacks like B"
else
puts "A doesn't quack like B"
end
# => "A quacks like B"
if B.new.quacks_like? A
puts "B quacks like A"
else
puts "B doesn't quack like A"
end
# => "A doesn't quack like B"
Basically, when I call obj.quacks_like? Klass I ask if every public
method defined in Klass is defined in obj as well.
Cheers,
Daniel
do this, but I'm interested in knowing whether the idea itself is...
well, stupid.
class Object
def quacks_like? (klass)
klass.public_instance_methods.each do |method|
return false unless respond_to? method
end
return true
end
end
class A
def foo; end
def bar; end
end
class B
def foo; end
end
if A.new.quacks_like? B
puts "A quacks like B"
else
puts "A doesn't quack like B"
end
# => "A quacks like B"
if B.new.quacks_like? A
puts "B quacks like A"
else
puts "B doesn't quack like A"
end
# => "A doesn't quack like B"
Basically, when I call obj.quacks_like? Klass I ask if every public
method defined in Klass is defined in obj as well.
Cheers,
Daniel