Skip to content

Instantly share code, notes, and snippets.

@louishuyng
Created November 11, 2023 06:36
Show Gist options
  • Save louishuyng/e8dd7d77992ba6196a213e4c66b0350b to your computer and use it in GitHub Desktop.
Save louishuyng/e8dd7d77992ba6196a213e4c66b0350b to your computer and use it in GitHub Desktop.
Ruby Monkey Patching Using Module
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