Skip to content

Instantly share code, notes, and snippets.

@KonnorRogers
Created April 5, 2019 19:27
Show Gist options
  • Save KonnorRogers/105cc64a60c04156ac18e0093f1d9bc7 to your computer and use it in GitHub Desktop.
Save KonnorRogers/105cc64a60c04156ac18e0093f1d9bc7 to your computer and use it in GitHub Desktop.
Extending a module
module ExtendMe
def extension_method
puts "Hello from ExtendMe!"
end
end
class Klass
extend ExtendMe
def self.extended
extension_method
end
end
Klass.extended
# The above code will not produce an error and instead will print:
# "Hello from ExtendMe!"
# I learned a very valueable lesson today about extending a module
module ExtendMe
def self.extension_method
puts "Hello from ExtendMe!"
end
end
class Klass
extend ExtendMe
def self.extended
extension_method
end
end
Klass.extended
## The above code will return an error saying local variable extension_method
# does not exits. This is because you cannot extend a module self.#{x} method
# You can only extend module methods that are instane methods like the in the correct_extend.rb file below
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment