# frozen_string_literal: false
# irb/inspector.rb - inspect methods
# $Release Version: 0.9.6$
# $Date: 2002/06/11 07:51:31 $
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
# Convenience method to create a new Inspector, using the given +inspect+
# proc, and optional +init+ proc and passes them to Inspector.new
# irb(main):001:0> ins = IRB::Inspector(proc{ |v| "omg! #{v}" })
# irb(main):001:0> IRB.CurrentContext.inspect_mode = ins # => omg! #<IRB::Inspector:0x007f46f7ba7d28>
# irb(main):001:0> "what?" #=> omg! what?
def IRB::Inspector(inspect, init = nil)
Inspector.new(inspect, init)
# In order to create your own custom inspector there are two things you
# 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?
# Default inspectors available to irb, this includes:
# +:pp+:: Using Kernel#pretty_inspect
# +:yaml+:: Using YAML.dump
# +:marshal+:: Using Marshal.dump
# Determines the inspector to use where +inspector+ is one of the keys passed
# during inspector definition.
def self.keys_with_inspector(inspector)
INSPECTORS.select{|k,v| v == inspector}.collect{|k, v| k}
# 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)
def self.def_inspector(key, arg=nil, &block)
inspector = IRB::Inspector(block, arg)
def_inspector(k, inspector)
INSPECTORS[key] = inspector
INSPECTORS[key.to_s] = inspector
INSPECTORS[key] = inspector
INSPECTORS[key.intern] = inspector
INSPECTORS[key] = inspector
# Creates a new inspector object, using the given +inspect_proc+ when
# output return values in irb.
def initialize(inspect_proc, init_proc = nil)
# Proc to call when the inspector is activated, good for requiring
# Proc to call when the input is evaluated and output in irb.
Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
Inspector.def_inspector([true, :p, :inspect]){|v|
if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
result = Color.colorize_code(result)
puts "(Object doesn't support #inspect)"
Inspector.def_inspector([:pp, :pretty_inspect], proc{require "pp"}){|v|
result = v.pretty_inspect.chomp
if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
result = Color.colorize_code(result)
Inspector.def_inspector([:yaml, :YAML], proc{require "yaml"}){|v|
puts "(can't dump yaml. use inspect)"
Inspector.def_inspector([:marshal, :Marshal, :MARSHAL, Marshal]){|v|