Last active
June 23, 2017 17:30
-
-
Save shivabhusal/3a427a141b1d0813c5b97175927943a8 to your computer and use it in GitHub Desktop.
Ruby creates an anonymous class with the name of the module you are including and puts it in the ancestors chain.
This file contains 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 Printable | |
def print(message) | |
puts message | |
end | |
end | |
class Animal | |
include Printable | |
def speak | |
print("##") | |
end | |
end | |
class Cow < Animal | |
def speak | |
print("MOO") | |
end | |
end | |
# > Cow.ancestors | |
# => [Cow, Animal, Printable, Object, PP::ObjectMixin, Kernel, BasicObject] | |
# 'superclass' method skips the Printable module | |
# > Animal.superclass | |
# => Object | |
cow = Cow.new | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment