diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dd470f1..b7689a0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,9 @@ * add `Pry::LastException` (#1145) #### Bug fixes, etc. +* update `Pry::CLIPPED_PRINT` to include a hex representation of object ID when printing a return value. (#1162) +* change second argument of `Pry.view_clip()` from Fixnum to Hash to support returning a string with or + without a hex representation of object ID. (#1162) * break up `pry/completion.rb` into `pry/bond_completer.rb` and `pry/input_completer.rb` * `Pry#last_exception=` supports exception objects who have been frozen (#1145) * `binding.pry` inside `.pryrc` file now works, with some limitations (@richo / #1118) diff --git a/lib/pry.rb b/lib/pry.rb index 322b637b..13e0cc43 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -38,7 +38,7 @@ class Pry # useful when playing with truly enormous objects CLIPPED_PRINT = proc do |output, value| - output.puts Pry.view_clip(value) + output.puts Pry.view_clip(value, id: true) end # Will only show the first line of the backtrace diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 2fd2fac5..a4bd3f86 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -158,25 +158,37 @@ class Pry REPLFileLoader.new(file_name).load end + # # An inspector that clips the output to `max_length` chars. # In case of > `max_length` chars the `# notation is used. # @param obj The object to view. - # @param max_length The maximum number of chars before clipping occurs. - # @return [String] The string representation of `obj`. - def self.view_clip(obj, max_length = 60) - if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max_length + # + # @param options [Hash] + # :max_length The maximum number of chars before clipping occurs. + # default is 60. + # + # :id boolean to indicate whether or not a hex reprsentation of the object ID + # is attached to the output when the length of inspect is greater than value + # of `:max_length`. default is false. + # + # @return [String] + # The string representation of `obj`. + # + def self.view_clip(obj, options = {}) + max = options.fetch :max_length, 60 + id = options.fetch :id, false + if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max obj.name.to_s elsif Pry.main == obj # special-case to support jruby. # fixed as of https://github.com/jruby/jruby/commit/d365ebd309cf9df3dde28f5eb36ea97056e0c039 # we can drop in the future. obj.to_s - elsif Pry.config.prompt_safe_objects.any? { |v| v === obj } && obj.inspect.length <= max_length + elsif Pry.config.prompt_safe_objects.any? { |v| v === obj } && obj.inspect.length <= max obj.inspect else - "#<#{obj.class}:0x%x>" % (obj.object_id << 1) + id == true ? "#<#{obj.class}:0x%x>" % (obj.object_id << 1) : "#<#{obj.class}>" end - rescue RescuableException "unknown" end diff --git a/spec/pry_defaults_spec.rb b/spec/pry_defaults_spec.rb index 4b2fde35..119fff22 100644 --- a/spec/pry_defaults_spec.rb +++ b/spec/pry_defaults_spec.rb @@ -97,7 +97,7 @@ describe "test Pry defaults" do Pry.print = new_print Pry.new.print.should == Pry.print - Object.new.pry :input => InputTester.new("\"test\""), :output => @str_output + Object.new.pry :input => InputTester.new("\"test\""), :output => @str_output @str_output.string.should == "=> LOL\n" @str_output = StringIO.new @@ -244,41 +244,43 @@ describe "test Pry defaults" do end describe "view_clip used for displaying an object in a truncated format" do - - VC_MAX_LENGTH = 60 + DEFAULT_OPTIONS = { + max_length: 60 + } + MAX_LENGTH = DEFAULT_OPTIONS[:max_length] describe "given an object with an #inspect string" do it "returns the #<> format of the object (never use inspect)" do o = Object.new - def o.inspect; "a" * VC_MAX_LENGTH; end + def o.inspect; "a" * MAX_LENGTH; end - Pry.view_clip(o, VC_MAX_LENGTH).should =~ /# format of the special-cased immediate object if #inspect is longer than maximum" do - o = "o" * (VC_MAX_LENGTH + 1) - Pry.view_clip(o, VC_MAX_LENGTH).should =~ /# format of the object (never use inspect)" do o = Object.new - def o.inspect; "a" * VC_MAX_LENGTH; end + def o.inspect; "a" * DEFAULT_OPTIONS; end - Pry.view_clip(o, VC_MAX_LENGTH).should =~ /# format" do o = Object.new - def o.inspect; "a" * (VC_MAX_LENGTH + 1); end + def o.inspect; "a" * (DEFAULT_OPTIONS + 1); end - Pry.view_clip(o, VC_MAX_LENGTH).should =~ /# format" do c, m = Class.new, Module.new - Pry.view_clip(c, VC_MAX_LENGTH).should =~ /# format" do c, m = Class.new, Module.new - def c.name; "a" * VC_MAX_LENGTH; end - def m.name; "a" * VC_MAX_LENGTH; end + def c.name; "a" * MAX_LENGTH; end + def m.name; "a" * MAX_LENGTH; end - Pry.view_clip(c, VC_MAX_LENGTH).should == c.name - Pry.view_clip(m, VC_MAX_LENGTH).should == m.name + Pry.view_clip(c, DEFAULT_OPTIONS).should == c.name + Pry.view_clip(m, DEFAULT_OPTIONS).should == m.name end end