Skip to content

Instantly share code, notes, and snippets.

@jferris
Created March 21, 2013 18:31
Show Gist options
  • Select an option

  • Save jferris/5215457 to your computer and use it in GitHub Desktop.

Select an option

Save jferris/5215457 to your computer and use it in GitHub Desktop.
class InspectorGadget < BasicObject
def initialize(target, notifier)
@target = target
@notifier = notifier
end
def method_missing(name, *args, &block)
result = @target.public_send(name, *args, &block)
@notifier.invocation(name, args, block, result)
result
rescue ::Exception => exception
@notifier.invocation(name, args, block, exception)
Kernel.raise
end
def respond_to_missing?(method_name, include_private = false)
@target.respond_to_missing?(method_name, include_private) || super
end
end
class PrintNotifier
def initialize(output)
@output = output
end
def invocation(name, args, block, result)
inspected_args = args.map(&:inspect).join(', ')
if block
inspected_args << "&#{block.inspect}"
end
@output.puts "#{name}(#{inspected_args})"
@output.puts "=> #{result.inspect}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment