-
-
Save mattetti/1328307 to your computer and use it in GitHub Desktop.
Interfacing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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