As an interface : interface « Class « Ruby






As an interface


class Duck
  def quack
    'Quack!'
  end

  def swim
    'Paddle paddle paddle...'
  end
end

class Goose
  def honk
    'Honk!'
  end

  def swim
    'Splash splash splash...'
  end
end

class DuckRecording
  def quack
    play
  end

  def play
    'Quack!'
  end
end
def make_it_quack(duck)
  duck.quack
end

make_it_quack(Duck.new)                   # => "Quack!"
make_it_quack(DuckRecording.new)
# TypeException: object not of type Duck

 








Related examples in the same category