Skip to content

Instantly share code, notes, and snippets.

@gabebw
Last active August 29, 2015 14:04
Show Gist options
  • Save gabebw/42ac6d4b1e5fc032ec43 to your computer and use it in GitHub Desktop.
Save gabebw/42ac6d4b1e5fc032ec43 to your computer and use it in GitHub Desktop.
Exploring modules and inheritance in Ruby
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