Created
December 19, 2011 10:48
-
-
Save wtaysom/1496598 to your computer and use it in GitHub Desktop.
_why's Ruby MethodFinder addition tweaked and revised.
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 '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