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

ruby-changes:46598

From: nobu <ko1@a...>
Date: Sun, 14 May 2017 20:45:08 +0900 (JST)
Subject: [ruby-changes:46598] nobu:r58714 (trunk): object.c: use a sized enumerator with #yield_self

nobu	2017-05-14 20:45:00 +0900 (Sun, 14 May 2017)

  New Revision: 58714

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58714

  Log:
    object.c: use a sized enumerator with #yield_self
    
    * object.c (rb_obj_size): The #yield_self Enumerator instance
      always has a #count of `1`.  This provides a lazy #size of `1`
      to match the count instead of `nil`.  [Fix GH-1615]
    
    Author:    Shannon Skipper <shannonskipper@g...>

  Modified files:
    trunk/object.c
    trunk/test/ruby/test_object.rb
Index: object.c
===================================================================
--- object.c	(revision 58713)
+++ object.c	(revision 58714)
@@ -497,6 +497,12 @@ rb_obj_itself(VALUE obj) https://github.com/ruby/ruby/blob/trunk/object.c#L497
     return obj;
 }
 
+static VALUE
+rb_obj_size(VALUE self, VALUE args, VALUE obj)
+{
+    return LONG2FIX(1);
+}
+
 /*
  *  call-seq:
  *     obj.yield_self {|_obj|...} -> an_object
@@ -510,7 +516,7 @@ rb_obj_itself(VALUE obj) https://github.com/ruby/ruby/blob/trunk/object.c#L516
 static VALUE
 rb_obj_yield_self(VALUE obj)
 {
-    RETURN_ENUMERATOR(obj, 0, 0);
+    RETURN_SIZED_ENUMERATOR(obj, 0, 0, rb_obj_size);
     return rb_yield_values2(1, &obj);
 }
 
Index: test/ruby/test_object.rb
===================================================================
--- test/ruby/test_object.rb	(revision 58713)
+++ test/ruby/test_object.rb	(revision 58714)
@@ -23,7 +23,9 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_object.rb#L23
     object = Object.new
     assert_same(self, object.yield_self {self}, feature)
     assert_same(object, object.yield_self {|x| break x}, feature)
-    assert_instance_of(Enumerator, object.yield_self)
+    enum = object.yield_self
+    assert_instance_of(Enumerator, enum)
+    assert_equal(1, enum.size)
   end
 
   def test_dup

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

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