Last active
August 29, 2015 14:04
-
-
Save gabebw/42ac6d4b1e5fc032ec43 to your computer and use it in GitHub Desktop.
Exploring modules and inheritance in Ruby
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 ModuleOne | |
def name | |
"Module 1" | |
end | |
end | |
module ModuleTwo | |
def name | |
"Module 2" | |
end | |
end | |
class Parent | |
def name | |
"Parent" | |
end | |
end | |
class Child < Parent | |
include ModuleOne | |
include ModuleTwo | |
# Without any modules, name is "Parent" | |
# With ModuleOne, name is "Module 1" | |
# With ModuleTwo included after ModuleOne, name is "Module 2" | |
# With ModuleOne included after ModuleTwo, name is "Module 1" again | |
def name | |
super | |
end | |
end | |
puts Child.new.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment