2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2009-07-08 08:01:44 -04:00
|
|
|
#
|
|
|
|
# irb/inspector.rb - inspect methods
|
|
|
|
# $Release Version: 0.9.6$
|
|
|
|
# $Revision: 1.19 $
|
|
|
|
# $Date: 2002/06/11 07:51:31 $
|
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2011-05-15 07:55:52 -04:00
|
|
|
#
|
2009-07-08 08:01:44 -04:00
|
|
|
#
|
|
|
|
|
2012-12-13 00:22:30 -05:00
|
|
|
module IRB # :nodoc:
|
2009-07-08 08:01:44 -04:00
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
|
|
|
|
# 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?
|
|
|
|
#
|
2009-07-08 08:01:44 -04:00
|
|
|
def IRB::Inspector(inspect, init = nil)
|
|
|
|
Inspector.new(inspect, init)
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# 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?
|
|
|
|
#
|
2009-07-08 08:01:44 -04:00
|
|
|
class Inspector
|
2012-12-21 12:08:24 -05:00
|
|
|
# Default inspectors available to irb, this includes:
|
|
|
|
#
|
|
|
|
# +:pp+:: Using Kernel#pretty_inspect
|
|
|
|
# +:yaml+:: Using YAML.dump
|
|
|
|
# +:marshal+:: Using Marshal.dump
|
|
|
|
INSPECTORS = {}
|
|
|
|
|
|
|
|
# Determines the inspector to use where +inspector+ is one of the keys passed
|
|
|
|
# during inspector definition.
|
|
|
|
def self.keys_with_inspector(inspector)
|
2012-12-26 09:56:57 -05:00
|
|
|
INSPECTORS.select{|k,v| v == inspector}.collect{|k, v| k}
|
2012-12-21 12:08:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
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
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Creates a new inspector object, using the given +inspect_proc+ when
|
|
|
|
# output return values in irb.
|
2009-07-08 08:01:44 -04:00
|
|
|
def initialize(inspect_proc, init_proc = nil)
|
|
|
|
@init = init_proc
|
|
|
|
@inspect = inspect_proc
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Proc to call when the inspector is activated, good for requiring
|
2013-11-30 04:50:11 -05:00
|
|
|
# dependent libraries.
|
2009-07-08 08:01:44 -04:00
|
|
|
def init
|
|
|
|
@init.call if @init
|
|
|
|
end
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# Proc to call when the input is evaluated and output in irb.
|
2009-07-08 08:01:44 -04:00
|
|
|
def inspect_value(v)
|
|
|
|
@inspect.call(v)
|
2021-01-07 05:21:06 -05:00
|
|
|
rescue
|
|
|
|
puts "(Object doesn't support #inspect)"
|
|
|
|
''
|
2009-07-08 08:01:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-21 12:08:24 -05:00
|
|
|
Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
|
2020-12-29 01:16:08 -05:00
|
|
|
Inspector.def_inspector([:p, :inspect]){|v|
|
2022-06-28 09:30:36 -04:00
|
|
|
Color.colorize_code(v.inspect, colorable: Color.colorable? && Color.inspect_colorable?(v))
|
2009-09-06 23:28:41 -04:00
|
|
|
}
|
2022-01-16 17:20:05 -05:00
|
|
|
Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require_relative "color_printer"}){|v|
|
2022-06-28 09:30:36 -04:00
|
|
|
IRB::ColorPrinter.pp(v, '').chomp
|
2019-04-25 08:36:48 -04:00
|
|
|
}
|
2012-12-21 12:08:24 -05:00
|
|
|
Inspector.def_inspector([:yaml, :YAML], proc{require "yaml"}){|v|
|
2009-07-08 08:01:44 -04:00
|
|
|
begin
|
|
|
|
YAML.dump(v)
|
|
|
|
rescue
|
|
|
|
puts "(can't dump yaml. use inspect)"
|
|
|
|
v.inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2012-12-21 12:08:24 -05:00
|
|
|
Inspector.def_inspector([:marshal, :Marshal, :MARSHAL, Marshal]){|v|
|
2009-07-08 08:01:44 -04:00
|
|
|
Marshal.dump(v)
|
|
|
|
}
|
|
|
|
end
|