Skip to content

Instantly share code, notes, and snippets.

@sk187
Created March 26, 2015 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sk187/dc18db333bfac4073d54 to your computer and use it in GitHub Desktop.
Save sk187/dc18db333bfac4073d54 to your computer and use it in GitHub Desktop.
Ruby Module: Include vs. Extend
# 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