summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-15 12:07:07 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-15 12:07:07 +0000
commite69dbe71b153f966e76ce4487d227ad0d0917628 (patch)
treed0c2c666d1a5f92b6c13657dc607a1f466427ff4
parent0cee9372b21e950ed66e2a74446773d96d698c60 (diff)
* lib/ostruct.rb (OpenStruct#inspect): fixed the recursion check.
Patch by Kornelius Kalnbach. [ruby-core:20992]. * test/ostruct/test_ostruct.rb: test for inspect. Patch by Kornelius Kalnbach. [ruby-core:20992]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@22329 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--lib/ostruct.rb24
-rw-r--r--test/ostruct/test_ostruct.rb14
3 files changed, 33 insertions, 13 deletions
diff --git a/ChangeLog b/ChangeLog
index b1a6ac5e49..e98686123c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Sun Feb 15 21:07:05 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * lib/ostruct.rb (OpenStruct#inspect): fixed the recursion check.
+ Patch by Kornelius Kalnbach. [ruby-core:20992].
+
+ * test/ostruct/test_ostruct.rb: test for inspect.
+ Patch by Kornelius Kalnbach. [ruby-core:20992].
+
Sat Feb 14 18:57:10 2009 Tanaka Akira <akr@fsij.org>
* eval.c (rb_thread_schedule): handle EBADF of select as well.
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index 6af5bbdac0..ba7f061a22 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -111,25 +111,23 @@ class OpenStruct
def inspect
str = "#<#{self.class}"
- Thread.current[InspectKey] ||= []
- if Thread.current[InspectKey].include?(self) then
- str << " ..."
- else
+ ids = (Thread.current[InspectKey] ||= [])
+ if ids.include?(object_id)
+ return str << ' ...>'
+ end
+
+ ids << object_id
+ begin
first = true
for k,v in @table
str << "," unless first
first = false
-
- Thread.current[InspectKey] << v
- begin
- str << " #{k}=#{v.inspect}"
- ensure
- Thread.current[InspectKey].pop
- end
+ str << " #{k}=#{v.inspect}"
end
+ return str << '>'
+ ensure
+ ids.pop
end
-
- str << ">"
end
alias :to_s :inspect
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb
index 24ed17f800..79662c6437 100644
--- a/test/ostruct/test_ostruct.rb
+++ b/test/ostruct/test_ostruct.rb
@@ -20,4 +20,18 @@ class TC_OpenStruct < Test::Unit::TestCase
o2.instance_eval{@table = {:a => 'b'}}
assert_not_equal(o1, o2)
end
+
+ def test_inspect
+ foo = OpenStruct.new
+ assert_equal("#<OpenStruct>", foo.inspect)
+ foo.bar = 1
+ foo.baz = 2
+ assert_equal("#<OpenStruct bar=1, baz=2>", foo.inspect)
+
+ foo = OpenStruct.new
+ foo.bar = OpenStruct.new
+ assert_equal('#<OpenStruct bar=#<OpenStruct>>', foo.inspect)
+ foo.bar.foo = foo
+ assert_equal('#<OpenStruct bar=#<OpenStruct foo=#<OpenStruct ...>>>', foo.inspect)
+ end
end