-
-
Save 0x1eef/d3224941c61846298be26396c7209d0b to your computer and use it in GitHub Desktop.
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
require 'digest/md5' | |
def mixin(obj) | |
m, k = Module.new {}, obj | |
k.methods.each do |x| | |
m.define_method(x, proc { |*args, &b| k.method(x).call(*args, &b) }) | |
end | |
m | |
end | |
class Foo | |
def initialize | |
@bar = 1 | |
end | |
def foobar | |
[self, @bar] | |
end | |
end | |
class Bar | |
include mixin(Foo.new) | |
end | |
bar = Bar.new | |
p bar.foobar | |
class Baz | |
include mixin(Digest::MD5) | |
end | |
baz = Baz.new | |
p baz.hexdigest('foo') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment