Created
November 22, 2018 17:38
-
-
Save showaltb/da9e8773211f9e7b62d2a52ec2047eb2 to your computer and use it in GitHub Desktop.
Example of using define_method in a mixin to define a method in an including class/module
This file contains hidden or 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 MyMixin | |
def self.included(other) | |
other.instance_eval do | |
define_method :bar do | |
puts 'bar called' | |
end | |
end | |
end | |
end | |
class MyClass | |
include MyMixin | |
def foo | |
puts 'foo called' | |
end | |
end | |
p MyClass.instance_methods(false) | |
# => [:bar, :foo] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment