Skip to content

Instantly share code, notes, and snippets.

@mattetti
Forked from etdebruin/gist:1328122
Created October 31, 2011 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattetti/1328307 to your computer and use it in GitHub Desktop.
Save mattetti/1328307 to your computer and use it in GitHub Desktop.
Interfacing
module Dog
def species
p "I'm a dog"
end
def race
p "I'm a #{self.class.name}"
end
# To implement in the interface implementation
#def bark
# raise "Your dog can't bar, because its #bark method is not implemented"
#end
[:legs, :bark, :age, :name].each do |meth|
define_method(meth){ raise "#{meth} must be implemented" }
end
end
class FrenchCaniche
include Dog
def bark
p "my bark sounds like: 'wouf wouf'"
end
end
class Wolf
include Dog
end
print "\nCaniche:\n"
rex = FrenchCaniche.new
rex.species
rex.race
rex.bark
print "\nWolf:\n"
jake = Wolf.new
jake.species
jake.race
jake.bark
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment