Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby27/share/ruby/irb
File: inspector.rb
# frozen_string_literal: false
[0] Fix | Delete
#
[1] Fix | Delete
# irb/inspector.rb - inspect methods
[2] Fix | Delete
# $Release Version: 0.9.6$
[3] Fix | Delete
# $Revision: 1.19 $
[4] Fix | Delete
# $Date: 2002/06/11 07:51:31 $
[5] Fix | Delete
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
[6] Fix | Delete
#
[7] Fix | Delete
# --
[8] Fix | Delete
#
[9] Fix | Delete
#
[10] Fix | Delete
#
[11] Fix | Delete
[12] Fix | Delete
module IRB # :nodoc:
[13] Fix | Delete
[14] Fix | Delete
[15] Fix | Delete
# Convenience method to create a new Inspector, using the given +inspect+
[16] Fix | Delete
# proc, and optional +init+ proc and passes them to Inspector.new
[17] Fix | Delete
#
[18] Fix | Delete
# irb(main):001:0> ins = IRB::Inspector(proc{ |v| "omg! #{v}" })
[19] Fix | Delete
# irb(main):001:0> IRB.CurrentContext.inspect_mode = ins # => omg! #<IRB::Inspector:0x007f46f7ba7d28>
[20] Fix | Delete
# irb(main):001:0> "what?" #=> omg! what?
[21] Fix | Delete
#
[22] Fix | Delete
def IRB::Inspector(inspect, init = nil)
[23] Fix | Delete
Inspector.new(inspect, init)
[24] Fix | Delete
end
[25] Fix | Delete
[26] Fix | Delete
# An irb inspector
[27] Fix | Delete
#
[28] Fix | Delete
# In order to create your own custom inspector there are two things you
[29] Fix | Delete
# should be aware of:
[30] Fix | Delete
#
[31] Fix | Delete
# Inspector uses #inspect_value, or +inspect_proc+, for output of return values.
[32] Fix | Delete
#
[33] Fix | Delete
# This also allows for an optional #init+, or +init_proc+, which is called
[34] Fix | Delete
# when the inspector is activated.
[35] Fix | Delete
#
[36] Fix | Delete
# Knowing this, you can create a rudimentary inspector as follows:
[37] Fix | Delete
#
[38] Fix | Delete
# irb(main):001:0> ins = IRB::Inspector.new(proc{ |v| "omg! #{v}" })
[39] Fix | Delete
# irb(main):001:0> IRB.CurrentContext.inspect_mode = ins # => omg! #<IRB::Inspector:0x007f46f7ba7d28>
[40] Fix | Delete
# irb(main):001:0> "what?" #=> omg! what?
[41] Fix | Delete
#
[42] Fix | Delete
class Inspector
[43] Fix | Delete
# Default inspectors available to irb, this includes:
[44] Fix | Delete
#
[45] Fix | Delete
# +:pp+:: Using Kernel#pretty_inspect
[46] Fix | Delete
# +:yaml+:: Using YAML.dump
[47] Fix | Delete
# +:marshal+:: Using Marshal.dump
[48] Fix | Delete
INSPECTORS = {}
[49] Fix | Delete
[50] Fix | Delete
# Determines the inspector to use where +inspector+ is one of the keys passed
[51] Fix | Delete
# during inspector definition.
[52] Fix | Delete
def self.keys_with_inspector(inspector)
[53] Fix | Delete
INSPECTORS.select{|k,v| v == inspector}.collect{|k, v| k}
[54] Fix | Delete
end
[55] Fix | Delete
[56] Fix | Delete
# Example
[57] Fix | Delete
#
[58] Fix | Delete
# Inspector.def_inspector(key, init_p=nil){|v| v.inspect}
[59] Fix | Delete
# Inspector.def_inspector([key1,..], init_p=nil){|v| v.inspect}
[60] Fix | Delete
# Inspector.def_inspector(key, inspector)
[61] Fix | Delete
# Inspector.def_inspector([key1,...], inspector)
[62] Fix | Delete
def self.def_inspector(key, arg=nil, &block)
[63] Fix | Delete
if block_given?
[64] Fix | Delete
inspector = IRB::Inspector(block, arg)
[65] Fix | Delete
else
[66] Fix | Delete
inspector = arg
[67] Fix | Delete
end
[68] Fix | Delete
[69] Fix | Delete
case key
[70] Fix | Delete
when Array
[71] Fix | Delete
for k in key
[72] Fix | Delete
def_inspector(k, inspector)
[73] Fix | Delete
end
[74] Fix | Delete
when Symbol
[75] Fix | Delete
INSPECTORS[key] = inspector
[76] Fix | Delete
INSPECTORS[key.to_s] = inspector
[77] Fix | Delete
when String
[78] Fix | Delete
INSPECTORS[key] = inspector
[79] Fix | Delete
INSPECTORS[key.intern] = inspector
[80] Fix | Delete
else
[81] Fix | Delete
INSPECTORS[key] = inspector
[82] Fix | Delete
end
[83] Fix | Delete
end
[84] Fix | Delete
[85] Fix | Delete
# Creates a new inspector object, using the given +inspect_proc+ when
[86] Fix | Delete
# output return values in irb.
[87] Fix | Delete
def initialize(inspect_proc, init_proc = nil)
[88] Fix | Delete
@init = init_proc
[89] Fix | Delete
@inspect = inspect_proc
[90] Fix | Delete
end
[91] Fix | Delete
[92] Fix | Delete
# Proc to call when the inspector is activated, good for requiring
[93] Fix | Delete
# dependent libraries.
[94] Fix | Delete
def init
[95] Fix | Delete
@init.call if @init
[96] Fix | Delete
end
[97] Fix | Delete
[98] Fix | Delete
# Proc to call when the input is evaluated and output in irb.
[99] Fix | Delete
def inspect_value(v)
[100] Fix | Delete
@inspect.call(v)
[101] Fix | Delete
end
[102] Fix | Delete
end
[103] Fix | Delete
[104] Fix | Delete
Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
[105] Fix | Delete
Inspector.def_inspector([true, :p, :inspect]){|v|
[106] Fix | Delete
begin
[107] Fix | Delete
result = v.inspect
[108] Fix | Delete
if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
[109] Fix | Delete
result = Color.colorize_code(result)
[110] Fix | Delete
end
[111] Fix | Delete
result
[112] Fix | Delete
rescue NoMethodError
[113] Fix | Delete
puts "(Object doesn't support #inspect)"
[114] Fix | Delete
''
[115] Fix | Delete
end
[116] Fix | Delete
}
[117] Fix | Delete
Inspector.def_inspector([:pp, :pretty_inspect], proc{require "pp"}){|v|
[118] Fix | Delete
result = v.pretty_inspect.chomp
[119] Fix | Delete
if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
[120] Fix | Delete
result = Color.colorize_code(result)
[121] Fix | Delete
end
[122] Fix | Delete
result
[123] Fix | Delete
}
[124] Fix | Delete
Inspector.def_inspector([:yaml, :YAML], proc{require "yaml"}){|v|
[125] Fix | Delete
begin
[126] Fix | Delete
YAML.dump(v)
[127] Fix | Delete
rescue
[128] Fix | Delete
puts "(can't dump yaml. use inspect)"
[129] Fix | Delete
v.inspect
[130] Fix | Delete
end
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
Inspector.def_inspector([:marshal, :Marshal, :MARSHAL, Marshal]){|v|
[134] Fix | Delete
Marshal.dump(v)
[135] Fix | Delete
}
[136] Fix | Delete
end
[137] Fix | Delete
[138] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function