diff --git a/ChangeLog b/ChangeLog index 7f4fc80800..c99a2195f0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sat May 13 16:14:05 2006 Tanaka Akira + + * lib/pp.rb (PP.mcall): new method. + (Struct#pretty_print): call Kernel#class and Struct#members even if + overriden. + (Struct#pretty_print_cycle): ditto. + Thu May 11 18:30:11 2006 GOTOU Yuuzou * ext/openssl/ossl_cipher.c (add_cipher_name_to_ary): should return diff --git a/lib/pp.rb b/lib/pp.rb index 62c98bd451..dc5856dab0 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -83,6 +83,12 @@ class PP < PrettyPrint out end + # :stopdoc: + def PP.mcall(obj, mod, meth, *args, &block) + mod.instance_method(meth).bind(obj).call(*args, &block) + end + # :startdoc: + @sharing_detection = false class << self # Returns the sharing detection flag as a boolean value. @@ -343,8 +349,8 @@ end class Struct def pretty_print(q) - q.group(1, '#') { - q.seplist(self.members, lambda { q.text "," }) {|member| + q.group(1, '#') { + q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member| q.breakable q.text member.to_s q.text '=' @@ -357,7 +363,7 @@ class Struct end def pretty_print_cycle(q) - q.text sprintf("#", self.class.name) + q.text sprintf("#", PP.mcall(self, Kernel, :class).name) end end @@ -496,6 +502,12 @@ if __FILE__ == $0 def test_list0123_11 assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], '', 11)) end + + OverriddenStruct = Struct.new("OverriddenStruct", :members, :class) + def test_struct_override_members # [ruby-core:7865] + a = OverriddenStruct.new(1,2) + assert_equal("#\n", PP.pp(a, '')) + end end class HasInspect