class IRB::Inspector
irb(main):001:0> “what?” #=> omg! what?
irb(main):001:0> IRB.CurrentContext.inspect_mode = ins # => omg! #<IRB::Inspector:0x007f46f7ba7d28>
irb(main):001:0> ins = IRB::Inspector.new(proc{ |v| “omg! #{v}” })
Knowing this, you can create a rudimentary inspector as follows:
when the inspector is activated.
This also allows for an optional #init+, or init_proc
, which is called
Inspector uses #inspect_value, or inspect_proc
, for output of return values.
should be aware of:
In order to create your own custom inspector there are two things you
An irb inspector
def self.def_inspector(key, arg=nil, &block)
Inspector.def_inspector(key, inspector)
Inspector.def_inspector([key1,..], init_p=nil){|v| v.inspect}
Inspector.def_inspector(key, init_p=nil){|v| v.inspect}
Example
def self.def_inspector(key, arg=nil, &block) if block_given? inspector = IRB::Inspector(block, arg) else inspector = arg end case key when Array for k in key def_inspector(k, inspector) end when Symbol INSPECTORS[key] = inspector INSPECTORS[key.to_s] = inspector when String INSPECTORS[key] = inspector INSPECTORS[key.intern] = inspector else INSPECTORS[key] = inspector end end
def self.keys_with_inspector(inspector)
Determines the inspector to use where +inspector+ is one of the keys passed
def self.keys_with_inspector(inspector) INSPECTORS.select{|k,v| v == inspector}.collect{|k, v| k} end
def init
Proc to call when the inspector is activated, good for requiring
def init @init.call if @init end
def initialize(inspect_proc, init_proc = nil)
Creates a new inspector object, using the given +inspect_proc+ when
def initialize(inspect_proc, init_proc = nil) @init = init_proc @inspect = inspect_proc end
def inspect_value(v)
def inspect_value(v) @inspect.call(v) rescue => e puts "An error occurred when inspecting the object: #{e.inspect}" begin puts "Result of Kernel#inspect: #{KERNEL_INSPECT.bind_call(v)}" '' rescue => e puts "An error occurred when running Kernel#inspect: #{e.inspect}" puts e.backtrace.join("\n") '' end end