class ItsATrap

An inspecting delegator.

Create a trap passing in any object of your choice.

Any time a method is called on the trap, it prints the method name, all its args, and the direct caller.

@example Did you know how basic operators work? Now you do!

trapped_int = ItsATrap.new(3)

trapped_int - 55
[:-, [55], nil, "..."]
=> -52

55 - trapped_int
[:coerce, [55], nil, "..."]
=> 52

- trapped_int
[:-@, [], nil, "..."]
=> -3

Public Class Methods

new(obj=::Object.new, show_ret=false) click to toggle source
# File lib/gorillib/utils/console.rb, line 26
def initialize(obj=::Object.new, show_ret=false)
  @obj        = obj
  @call_count = 0
  @show_ret   = show_ret
end

Public Instance Methods

!=( *args, &block) click to toggle source
# File lib/gorillib/utils/console.rb, line 49
def !=(    *args, &block)        ; __describe_and_send__(:!=,     *args, &block) ; end
==( *args, &block) click to toggle source

These are defined on BasicObject, delegate them along with the rest

BasicObject.instance_methods
=> [:==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
# File lib/gorillib/utils/console.rb, line 46
def ==(    *args, &block)        ; __describe_and_send__(:==,     *args, &block) ; end
__obj__() click to toggle source

@return the proxied object

# File lib/gorillib/utils/console.rb, line 40
def __obj__ ; @obj ; end
equal?(*args, &block) click to toggle source
# File lib/gorillib/utils/console.rb, line 47
def equal?(*args, &block)        ; __describe_and_send__(:equal?, *args, &block) ; end
inspect() click to toggle source

We implement to_s and inspect, because otherwise it’s annoyingly noisy. :pretty_inspect makes pry happy.

# File lib/gorillib/utils/console.rb, line 34
def inspect() "~#{@obj.inspect}~" ; end
Also aliased as: pretty_inspect
instance_eval(*args, &block) click to toggle source
# File lib/gorillib/utils/console.rb, line 50
def instance_eval(*args, &block) ; __describe_and_send__(:instance_eval, *args, &block) ; end
instance_exec(*args, &block) click to toggle source
# File lib/gorillib/utils/console.rb, line 51
def instance_exec(*args, &block) ; __describe_and_send__(:instance_exec, *args, &block) ; end
methods() click to toggle source
# File lib/gorillib/utils/console.rb, line 37
def methods() @obj.methods ; end
pretty_inspect()
Alias for: inspect
to_s() click to toggle source
# File lib/gorillib/utils/console.rb, line 35
def to_s()    @obj.to_s        ; end

Private Instance Methods

__describe_and_send__(meth, *args, &block) click to toggle source
# File lib/gorillib/utils/console.rb, line 63
def __describe_and_send__(meth, *args, &block)
  pref         = "%-3d %-14s %-15s" % [@call_count, @obj.__id__, self.to_s[0..14]]
  @call_count += 1
  $stderr.puts   "%s %-15s <-  %-30s %s -- %s" % [pref, meth.to_s[0..14], args.map(&:inspect).join(','), block, ::Kernel.caller.first]
  ret = @obj.__send__(meth, *args, &block)
  $stderr.puts   "%s %-15s  -> %s"             % [pref, meth.to_s[0..14], ret.inspect] if @show_ret
  ret
end
method_missing(meth, *args, &block) click to toggle source

Any time a method is called on the trap, it prints the method name, all its args, and the direct caller.

# File lib/gorillib/utils/console.rb, line 59
def method_missing(meth, *args, &block)
  __describe_and_send__(meth, *args, &block)
end