Created
April 5, 2019 19:27
-
-
Save KonnorRogers/105cc64a60c04156ac18e0093f1d9bc7 to your computer and use it in GitHub Desktop.
Extending a module
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
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!" |
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
# 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