Class: IRB::Inspector
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 collapse
- KERNEL_INSPECT =
Object.instance_method(:inspect)
- INSPECTORS =
Default inspectors available to irb, this includes:
:pp
-
Using Kernel#pretty_inspect
:yaml
-
Using YAML.dump
:marshal
-
Using Marshal.dump
{}
Class Method Summary collapse
-
.def_inspector(key, arg = nil, &block) ⇒ Object
Example.
-
.keys_with_inspector(inspector) ⇒ Object
Determines the inspector to use where
inspector
is one of the keys passed during inspector definition.
Instance Method Summary collapse
-
#init ⇒ Object
Proc to call when the inspector is activated, good for requiring dependent libraries.
-
#initialize(inspect_proc, init_proc = nil) ⇒ Inspector
constructor
Creates a new inspector object, using the given
inspect_proc
when output return values in irb. -
#inspect_value(v, output, colorize: true) ⇒ Object
Proc to call when the input is evaluated and output in irb.
- #support_stream_output? ⇒ Boolean
Constructor Details
permalink #initialize(inspect_proc, init_proc = nil) ⇒ Inspector
Creates a new inspector object, using the given inspect_proc
when output return values in irb.
84 85 86 87 |
# File 'lib/irb/inspector.rb', line 84 def initialize(inspect_proc, init_proc = nil) @init = init_proc @inspect = inspect_proc end |
Class Method Details
permalink .def_inspector(key, arg = nil, &block) ⇒ Object
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)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/irb/inspector.rb', line 58 def 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 |
permalink .keys_with_inspector(inspector) ⇒ Object
Determines the inspector to use where inspector
is one of the keys passed during inspector definition.
48 49 50 |
# File 'lib/irb/inspector.rb', line 48 def keys_with_inspector(inspector) INSPECTORS.select{|k, v| v == inspector}.collect{|k, v| k} end |
Instance Method Details
permalink #init ⇒ Object
Proc to call when the inspector is activated, good for requiring dependent libraries.
91 92 93 |
# File 'lib/irb/inspector.rb', line 91 def init @init.call if @init end |
permalink #inspect_value(v, output, colorize: true) ⇒ Object
Proc to call when the input is evaluated and output in irb.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/irb/inspector.rb', line 101 def inspect_value(v, output, colorize: true) support_stream_output? ? @inspect.call(v, output, colorize: colorize) : output << @inspect.call(v, colorize: colorize) 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 |
permalink #support_stream_output? ⇒ Boolean
95 96 97 98 |
# File 'lib/irb/inspector.rb', line 95 def support_stream_output? second_parameter_type = @inspect.parameters[1]&.first second_parameter_type == :req || second_parameter_type == :opt end |