diff options
| author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-05-13 07:35:50 +0000 |
|---|---|---|
| committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-05-13 07:35:50 +0000 |
| commit | bf1b28acafb03ae181e803ef5ab4158d0be5f209 (patch) | |
| tree | 922d6036d3472b69ed9bd73aaec7b7ec2b630918 /lib | |
| parent | 11ba3e2331952f17dd5391b32db0964b4c29ddb9 (diff) | |
| download | ruby-bf1b28acafb03ae181e803ef5ab4158d0be5f209.tar.gz ruby-bf1b28acafb03ae181e803ef5ab4158d0be5f209.tar.xz ruby-bf1b28acafb03ae181e803ef5ab4158d0be5f209.zip | |
* lib/pp.rb (PP.mcall): new method.
(Struct#pretty_print): call Kernel#class and Struct#members even if
overriden.
(Struct#pretty_print_cycle): ditto.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8@10148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pp.rb | 18 |
1 files changed, 15 insertions, 3 deletions
@@ -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. @@ -315,8 +321,8 @@ end class Struct def pretty_print(q) - q.group(1, '#<struct ' + self.class.name, '>') { - q.seplist(self.members, lambda { q.text "," }) {|member| + q.group(1, '#<struct ' + PP.mcall(self, Kernel, :class).name, '>') { + q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member| q.breakable q.text member.to_s q.text '=' @@ -329,7 +335,7 @@ class Struct end def pretty_print_cycle(q) - q.text sprintf("#<struct %s:...>", self.class.name) + q.text sprintf("#<struct %s:...>", PP.mcall(self, Kernel, :class).name) end end @@ -468,6 +474,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("#<struct Struct::OverriddenStruct members=1, class=2>\n", PP.pp(a, '')) + end end class HasInspect |
