Created
March 26, 2015 19:23
-
-
Save sk187/dc18db333bfac4073d54 to your computer and use it in GitHub Desktop.
Ruby Module: Include vs. Extend
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
# Include give you access to a module on a instance level while | |
# extend give class level access. | |
module CanYouHearMe | |
def test | |
puts "I hear you loud and clear!" | |
end | |
end | |
class Radio | |
include CanYouHearMe | |
end | |
class Satellite | |
extend CanYouHearMe | |
end | |
# Calling the method test on the class Radio | |
Radio.test | |
# Returns | |
"private method 'test' called for Context::Radio:Class" | |
# Calling the method test on the class Satellite | |
Satellite.test | |
# Returns | |
"I hear you loud and clear!" | |
# Calling the method test on an instance of the class Radio | |
ham_radio = Radio.new | |
ham_radio.test | |
# Returns | |
"I hear you loud and clear!" | |
# Calling the method test on an instance of the class Satellite | |
sat_comm = Satellite.new | |
sat_comm.test | |
# Returns | |
"private method 'test' called for #<Context::Satellite:0x000000024ef2f8>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment