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

ruby-changes:46416

From: nobu <ko1@a...>
Date: Mon, 1 May 2017 16:51:00 +0900 (JST)
Subject: [ruby-changes:46416] nobu:r58528 (trunk): object.c: Kernel#yield_self

nobu	2017-05-01 16:50:53 +0900 (Mon, 01 May 2017)

  New Revision: 58528

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

  Log:
    object.c: Kernel#yield_self
    
    * object.c (rb_obj_yield_self): new method which yields the
      receiver and returns the result.
      [ruby-core:46320] [Feature #6721]

  Modified files:
    trunk/NEWS
    trunk/object.c
    trunk/test/ruby/test_object.rb
Index: NEWS
===================================================================
--- NEWS	(revision 58527)
+++ NEWS	(revision 58528)
@@ -40,6 +40,10 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L40
 
   * exception message "stream closed" is changed  [Bug #13405]
 
+* Kernel
+
+  * Kernel#yield_self  [Feature #6721]
+
 * Numeric
 
   * Numerical comparison operators (<,<=,>=,>) no longer rescue exceptions
Index: object.c
===================================================================
--- object.c	(revision 58527)
+++ object.c	(revision 58528)
@@ -497,6 +497,23 @@ rb_obj_itself(VALUE obj) https://github.com/ruby/ruby/blob/trunk/object.c#L497
     return obj;
 }
 
+/*
+ *  call-seq:
+ *     obj.yield_self {|_obj|...} -> an_object
+ *
+ *  Yields <i>obj</i> and returns the result.
+ *
+ *	'my string'.yield_self {|s|s.upcase} #=> "MY STRING"
+ *
+ */
+
+static VALUE
+rb_obj_yield_self(VALUE obj)
+{
+    RETURN_ENUMERATOR(obj, 0, 0);
+    return rb_yield_values2(1, &obj);
+}
+
 /* :nodoc: */
 VALUE
 rb_obj_init_copy(VALUE obj, VALUE orig)
@@ -3507,6 +3524,7 @@ InitVM_Object(void) https://github.com/ruby/ruby/blob/trunk/object.c#L3524
     rb_define_method(rb_mKernel, "clone", rb_obj_clone2, -1);
     rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
     rb_define_method(rb_mKernel, "itself", rb_obj_itself, 0);
+    rb_define_method(rb_mKernel, "yield_self", rb_obj_yield_self, 0);
     rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
     rb_define_method(rb_mKernel, "initialize_dup", rb_obj_init_dup_clone, 1);
     rb_define_method(rb_mKernel, "initialize_clone", rb_obj_init_dup_clone, 1);
Index: test/ruby/test_object.rb
===================================================================
--- test/ruby/test_object.rb	(revision 58527)
+++ test/ruby/test_object.rb	(revision 58528)
@@ -18,6 +18,14 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_object.rb#L18
     assert_same(object, object.itself, feature6373)
   end
 
+  def test_yield_self
+    feature = '[ruby-core:46320] [Feature #6721]'
+    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)
+  end
+
   def test_dup
     assert_equal 1, 1.dup
     assert_equal true, true.dup

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

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