Class: IRB::Inspector
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/irb/inspector.rb |
Overview
An irb inspector
In order to create your own custom inspector there are two things you should be aware of:
Inspector
uses #inspect_value, or inspect_proc
, for output of return values.
This also allows for an optional #init+, or init_proc
, which is called when the inspector is activated.
Knowing this, you can create a rudimentary inspector as follows:
irb(main):001:0> ins = IRB::Inspector.new(proc{ |v| "omg! #{v}" })
irb(main):001:0> IRB.CurrentContext.inspect_mode = ins # => omg! #<IRB::Inspector:0x007f46f7ba7d28>
irb(main):001:0> "what?" #=> omg! what?
Constant Summary
-
INSPECTORS =
Default inspectors available to irb, this includes:
:pp
-
Using
Kernel.pretty_inspect
:yaml
-
Using
YAML.dump
:marshal
-
Using
Marshal.dump
{}
Class Method Summary
-
.def_inspector(key, arg = nil, &block)
Example.
-
.keys_with_inspector(inspector)
Determines the inspector to use where
inspector
is one of the keys passed during inspector definition. -
.new(inspect_proc, init_proc = nil) ⇒ Inspector
constructor
Creates a new inspector object, using the given
inspect_proc
when output return values in irb.
Instance Method Summary
-
#init
Proc to call when the inspector is activated, good for requiring dependent libraries.
-
#inspect_value(v)
Proc to call when the input is evaluated and output in irb.
Constructor Details
.new(inspect_proc, init_proc = nil) ⇒ Inspector
Creates a new inspector object, using the given inspect_proc
when output return values in irb.
# File 'lib/irb/inspector.rb', line 88
def initialize(inspect_proc, init_proc = nil) @init = init_proc @inspect = inspect_proc end
Class Method Details
.def_inspector(key, arg = nil, &block)
Example
Inspector.def_inspector(key, init_p=nil){|v| v.inspect}
Inspector.def_inspector([key1,..], init_p=nil){|v| v.inspect}
Inspector.def_inspector(key, inspector)
Inspector.def_inspector([key1,...], inspector)
# File 'lib/irb/inspector.rb', line 63
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
.keys_with_inspector(inspector)
Determines the inspector to use where inspector
is one of the keys passed during inspector definition.
# File 'lib/irb/inspector.rb', line 53
def self.keys_with_inspector(inspector) INSPECTORS.select{|k,v| v == inspector}.collect{|k, v| k} end
Instance Method Details
#init
Proc to call when the inspector is activated, good for requiring dependent libraries.
# File 'lib/irb/inspector.rb', line 95
def init @init.call if @init end
#inspect_value(v)
Proc to call when the input is evaluated and output in irb.
# File 'lib/irb/inspector.rb', line 100
def inspect_value(v) @inspect.call(v) end