Maintenance of Ruby 2.0.0 will end on February 24, 2016. Read more

In Files

  • irb/inspector.rb


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 ={ |v| "omg! #{v}" })
irb(main):001:0> IRB.CurrentContext.inspect_mode = ins # => omg! #<IRB::Inspector:0x007f46f7ba7d28>
irb(main):001:0> "what?" #=> omg! what?



Default inspectors available to irb, this includes:


Using Kernel#pretty_inspect


Using YAML.dump


Using Marshal.dump

Public Class Methods

def_inspector(key, arg=nil, &block) click to toggle source


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 irb/inspector.rb, line 63
  def self.def_inspector(key, arg=nil, &block)
#     if !block_given?
#       case arg
#       when nil, Proc
#     inspector = IRB::Inspector(init_p)
#       when Inspector
#     inspector = init_p
#       else
#     IRB.Raise IllegalParameter, init_p
#       end
#       init_p = nil
#     else
#       inspector = IRB::Inspector(block, init_p)
#     end

    if block_given?
      inspector = IRB::Inspector(block, arg)
      inspector = arg

    case key
    when Array
      for k in key
        def_inspector(k, inspector)
    when Symbol
      INSPECTORS[key] = inspector
      INSPECTORS[key.to_s] = inspector
    when String
      INSPECTORS[key] = inspector
      INSPECTORS[key.intern] = inspector
      INSPECTORS[key] = inspector
keys_with_inspector(inspector) click to toggle source

Determines the inspector to use where inspector is one of the keys passed during inspector definition.

               # File irb/inspector.rb, line 53
def self.keys_with_inspector(inspector){|k,v| v == inspector}.collect{|k, v| k}
new(inspect_proc, init_proc = nil) click to toggle source

Creates a new inspector object, using the given inspect_proc when output return values in irb.

               # File irb/inspector.rb, line 102
def initialize(inspect_proc, init_proc = nil)
  @init = init_proc
  @inspect = inspect_proc

Public Instance Methods

init() click to toggle source

Proc to call when the inspector is activated, good for requiring dependant libraries.

               # File irb/inspector.rb, line 109
def init if @init
inspect_value(v) click to toggle source

Proc to call when the input is evaluated and output in irb.

               # File irb/inspector.rb, line 114
def inspect_value(v)

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit

blog comments powered by Disqus