Skip to content

Instantly share code, notes, and snippets.

@wtaysom
Created December 19, 2011 10:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wtaysom/1496598 to your computer and use it in GitHub Desktop.
Save wtaysom/1496598 to your computer and use it in GitHub Desktop.
_why's Ruby MethodFinder addition tweaked and revised.
require 'methodfinder'
# _why's MethodFinder addition tweaked and revised.
# <http://redhanded.hobix.com/inspect/stickItInYourIrbrcMethodfinder.html>
class MethodFinder
def self.with_redirected_streams
redirect_streams
yield
ensure
restore_streams
end
module What
def what?(*args, &block)
Delegate.new(self, args, &block)
end
class Delegate < BasicObject
undef_method :!
undef_method :!=
undef_method :==
undef_method :equal?
def initialize(receiver, args, &block)
@receiver = receiver
@args = args
@block = block
end
def pretty_inspect
args = @args.pretty_inspect
args.sub!(/^\[/, '')
args.sub!(/\]\n$/, '')
block = @block ? "{}" : ""
"#{@receiver.pretty_inspect.chomp}.what?(#{args})#{block}"
end
def method_missing(m, *args, &block)
::MethodFinder.with_redirected_streams do
@receiver.find_method do |r|
r.unknown(*@args, &@block).send(m, *args, &block)
end
end
end
end
end
end
include MethodFinder::What
MethodFinder::INSTANCE_METHOD_BLACKLIST[:Object] << :what?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment