From f345126746242a7269654a0d9db64f0518462f41 Mon Sep 17 00:00:00 2001 From: akr Date: Mon, 20 Jan 2003 17:58:24 +0000 Subject: * lib/pp.rb: Don't redefine `inspect'. (pretty_print_inspect): new method. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3380 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 ++++ lib/pp.rb | 99 ++++++++++++++++++++++----------------------------------------- 2 files changed, 39 insertions(+), 65 deletions(-) diff --git a/ChangeLog b/ChangeLog index adf4b5a93c..2c43e17c9b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Jan 21 02:55:10 2003 Tanaka Akira + + * lib/pp.rb: Don't redefine `inspect'. + (pretty_print_inspect): new method. + Mon Jan 20 21:48:43 2003 Akinori MUSHA * configure.in (MANTYPE): Detect if the system's nroff(1) groks diff --git a/lib/pp.rb b/lib/pp.rb index 3718840680..0ff1456285 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -6,47 +6,25 @@ == Which seems better? non-pretty-printed output by (({p})) is: - #, @nest=[0], @newline="\n", @buf=#, #], @singleline_width=1>, #, #, @newline="\n", @indent=1, @tail=2, @sep=" ", @width=1>, #], @singleline_width=1>, #], @singleline_width=6>], @singleline_width=6>, @sharing_detection=false> + #, @group_queue=#], []]>, @buffer=[], @newline="\n", @group_stack=[#], @buffer_width=0, @indent=0, @maxwidth=79, @output_width=2, @output=#> pretty-printed output by (({pp})) is: - #, - #], - @group=2, - @singleline_width=1, - @tail=1>, - #, - #, - @group=2, - @indent=1, - @newline="\n", - @sep=" ", - @tail=2, - @width=1>, - #], - @group=2, - @singleline_width=1, - @tail=1>, - #], - @group=1, - @singleline_width=6, - @tail=0>], - @group=0, - @singleline_width=6, - @tail=0>, - @genspace=#, - @nest=[0], + #, + @group_queue= + #], + []]>, + @group_stack= + [#], + @indent=0, + @maxwidth=79, @newline="\n", - @sharing_detection=false, - @stack=[]> + @output=#, + @output_width=2> I like the latter. If you do too, this library is for you. @@ -131,6 +109,12 @@ PP#pp to print the object. This method should return an array of names of instance variables as symbols or strings as: (({[:@a, :@b]})). + +--- pretty_print_inspect + is (({inspect})) implementation using (({pretty_print})). + If you implement (({pretty_print})), it can be used as follows. + + alias inspect pretty_print_inspect =end require 'prettyprint' @@ -254,38 +238,16 @@ class PP < PrettyPrint module ObjectMixin # 1. specific pretty_print - # 2. specific inspect + # 2. specific inspect or to_s # 3. generic pretty_print - Key = :__pp_instead_of_inspect__ - def pretty_print(pp) # specific pretty_print is not defined, try specific inspect. - begin - old = Thread.current[Key] - result1 = sprintf('#<%s:0x%x pretty_printed>', self.class.name, self.__id__ * 2) - Thread.current[Key] = [pp, result1] - result2 = ObjectMixin.pp_call_inspect(self) - ensure - Thread.current[Key] = old - end - - unless result1.equal? result2 - pp.text result2 - end - end - - def ObjectMixin.pp_call_inspect(obj); obj.inspect; end - CallInspectFrame = "#{__FILE__}:#{__LINE__ - 1}:in `pp_call_inspect'" - - def inspect - if CallInspectFrame == caller[0] - # specific inspect is also not defined, use generic pretty_print. - pp, result = Thread.current[Key] - pp.pp_object(self) - result + if /\(Kernel\)#/ !~ method(:inspect).inspect || + /\(Kernel\)#/ !~ method(:to_s).inspect + pp.text inspect else - super + pp.pp_object(self) end end @@ -299,6 +261,13 @@ class PP < PrettyPrint def pretty_print_instance_variables instance_variables.sort end + + def pretty_print_inspect + if /\(PP::ObjectMixin\)#/ =~ method(:pretty_print).inspect + raise "pretty_print is not overriden." + end + PP.singleline_pp(self, '') + end end end -- cgit v1.2.3