Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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