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

ruby-changes:53057

From: aycabta <ko1@a...>
Date: Sun, 21 Oct 2018 16:15:49 +0900 (JST)
Subject: [ruby-changes:53057] aycabta:r65271 (trunk): Improve doc of yield_self

aycabta	2018-10-21 16:15:44 +0900 (Sun, 21 Oct 2018)

  New Revision: 65271

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

  Log:
    Improve doc of yield_self
    
    * object.c: Add code samples for yield_self.

  Modified files:
    trunk/object.c
Index: object.c
===================================================================
--- object.c	(revision 65270)
+++ object.c	(revision 65271)
@@ -570,6 +570,25 @@ rb_obj_size(VALUE self, VALUE args, VALU https://github.com/ruby/ruby/blob/trunk/object.c#L570
  *     3.next.then {|x| x**x }.to_s             #=> "256"
  *     "my string".yield_self {|s| s.upcase }   #=> "MY STRING"
  *
+ *  Good usage for +yield_self+ is values piping in long method
+ *  chains:
+ *
+ *     require 'open-uri'
+ *     require 'json'
+ *
+ *     construct_url(arguments).
+ *       yield_self { |url| open(url).read }.
+ *       yield_self { |response| JSON.parse(response) }
+ *
+ *  When called without block, the method returns +Enumerator+,
+ *  which can be used, for example, for conditional
+ *  circuit-breaking:
+ *
+ *     # meets condition, no-op
+ *     1.yield_self.detect(&:odd?)            # => 1
+ *     # does not meet condition, drop value
+ *     2.yeild_self.detect(&:odd?)            # => nil
+ *
  */
 
 static VALUE

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

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