Created
November 11, 2023 06:36
-
-
Save louishuyng/e8dd7d77992ba6196a213e4c66b0350b to your computer and use it in GitHub Desktop.
Ruby Monkey Patching Using 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 M | |
def test_method_with_module | |
"Test from M" | |
end | |
end | |
class C | |
def test_method | |
"Test from C" | |
end | |
end | |
# for a plugin, these two lines would go in init.rb | |
C.send(:include, M) | |
C.class_eval { alias_method_chain :test_method, :module } | |
C.new.test_method # => "Test from M" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment