Class: ItsATrap

Inherits:
BasicObject
Defined in:
lib/gorillib/utils/console.rb

Overview

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.

Examples:

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

Instance Method Summary collapse

Constructor Details

#initialize(obj = ::Object.new, show_ret = false) ⇒ ItsATrap

Returns a new instance of ItsATrap.



26
27
28
29
30
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)

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



59
60
61
# File 'lib/gorillib/utils/console.rb', line 59

def method_missing(meth, *args, &block)
  __describe_and_send__(meth, *args, &block)
end

Instance Method Details

#!=(*args, &block) ⇒ Object



49
# File 'lib/gorillib/utils/console.rb', line 49

def !=(    *args, &block)        ; __describe_and_send__(:!=,     *args, &block) ; end

#!@(*args, &block) ⇒ Object



48
# File 'lib/gorillib/utils/console.rb', line 48

def !@(    *args, &block)        ; __describe_and_send__(:!,      *args, &block) ; end

#==(*args, &block) ⇒ Object

These are defined on BasicObject, delegate them along with the rest BasicObject.instance_methods => [:==, :equal?, :!, :!=, :instance_eval, :instance_exec, :send, :id]



46
# File 'lib/gorillib/utils/console.rb', line 46

def ==(    *args, &block)        ; __describe_and_send__(:==,     *args, &block) ; end

#__obj__Object

Returns the proxied object.

Returns:

  • the proxied object



40
# File 'lib/gorillib/utils/console.rb', line 40

def __obj__ ; @obj ; end

#equal?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


47
# File 'lib/gorillib/utils/console.rb', line 47

def equal?(*args, &block)        ; __describe_and_send__(:equal?, *args, &block) ; end

#inspectObject Also known as: pretty_inspect

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



34
# File 'lib/gorillib/utils/console.rb', line 34

def inspect() "~#{@obj.inspect}~" ; end

#instance_eval(*args, &block) ⇒ Object



50
# 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) ⇒ Object



51
# File 'lib/gorillib/utils/console.rb', line 51

def instance_exec(*args, &block) ; __describe_and_send__(:instance_exec, *args, &block) ; end

#methodsObject



37
# File 'lib/gorillib/utils/console.rb', line 37

def methods() @obj.methods ; end

#to_sObject



35
# File 'lib/gorillib/utils/console.rb', line 35

def to_s()    @obj.to_s        ; end