[前][次][番号順一覧][スレッド一覧]

ruby-changes:10013

From: yugui <ko1@a...>
Date: Fri, 16 Jan 2009 00:39:13 +0900 (JST)
Subject: [ruby-changes:10013] Ruby:r21555 (ruby_1_9_1): merges r21496 from trunk into ruby_1_9_1.

yugui	2009-01-16 00:38:08 +0900 (Fri, 16 Jan 2009)

  New Revision: 21555

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=21555

  Log:
    merges r21496 from trunk into ruby_1_9_1.
    * 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].

  Modified files:
    branches/ruby_1_9_1/ChangeLog
    branches/ruby_1_9_1/lib/ostruct.rb
    branches/ruby_1_9_1/test/ostruct/test_ostruct.rb

Index: ruby_1_9_1/ChangeLog
===================================================================
--- ruby_1_9_1/ChangeLog	(revision 21554)
+++ ruby_1_9_1/ChangeLog	(revision 21555)
@@ -1,7 +1,15 @@
+Tue Jan 13 21:45:53 2009  Yuki Sonoda (Yugui)  <yugui@y...>
+
+	* 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].
+
 Tue Jan 13 21:28:14 2009  Yuki Sonoda (Yugui)  <yugui@y...>
 
 	* object.c (rb_obj_not_match): rdoc.
-	  Patch by Kornelius Kalnbach.
+	  Patch by Kornelius Kalnbach.  [ruby-core:20991]
 
 Tue Jan 13 21:44:30 2009  NAKAMURA Usaku  <usa@r...>
 
Index: ruby_1_9_1/lib/ostruct.rb
===================================================================
--- ruby_1_9_1/lib/ostruct.rb	(revision 21554)
+++ ruby_1_9_1/lib/ostruct.rb	(revision 21555)
@@ -112,25 +112,23 @@
   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
 
Index: ruby_1_9_1/test/ostruct/test_ostruct.rb
===================================================================
--- ruby_1_9_1/test/ostruct/test_ostruct.rb	(revision 21554)
+++ ruby_1_9_1/test/ostruct/test_ostruct.rb	(revision 21555)
@@ -20,4 +20,18 @@
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]