Created
January 9, 2013 04:15
-
-
Save mboeh/4490529 to your computer and use it in GitHub Desktop.
Gillian, please use extreme caution.
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
class Snatcher < BasicObject | |
C = ::Object.new | |
class << C | |
def abduct_method(meth, arity) | |
ObjectSpace.each_object(Module) do |mod| | |
next if mod.kind_of? Class | |
if mod.instance_methods.include?(meth) | |
return mod.instance_method(meth) | |
end | |
end | |
nil | |
end | |
end | |
def method_missing(meth, *args, &blk) | |
if method = C.abduct_method(meth, args.length) | |
(class << self; self; end).send(:define_method, meth, method) | |
__send__(meth, *args, &blk) | |
else | |
super | |
end | |
end | |
end | |
# Sad face: you can't abduct UnboundMethods from classes... as far as I can tell so far. | |
module Abductee | |
def bite | |
scream | |
end | |
end | |
module Abductee2 | |
def scream | |
"ow!" | |
end | |
end | |
# I've got you now, SNATCHER scum! | |
puts Snatcher.new.bite |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment