10 Things Every Java Programmer Should Know About Ruby

Item #5
Don’t Worry About Interfaces

Ruby Uses Duck Typing

  • If it walks like a duck,
  • And talks like a duck,
    • Then we can treat it like a duck.
    • (who cares what it really is)

class Duck
  def talk() puts "Quack" end
end
class DuckLikeObject
  def talk() puts "Kwak" end
end
flock = [
  Duck.new,
  DuckLikeObject.new ] 
flock.each do |d| d.talk end

No need to inherit from a common interface.