Skip to content

Instantly share code, notes, and snippets.

@mboeh
Created January 9, 2013 04:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mboeh/4490529 to your computer and use it in GitHub Desktop.
Save mboeh/4490529 to your computer and use it in GitHub Desktop.
Gillian, please use extreme caution.
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