summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraycabta <aycabta@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-10-21 07:15:44 +0000
committeraycabta <aycabta@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-10-21 07:15:44 +0000
commitd67b903519e1630d462bff7212a72397768bb677 (patch)
tree6162f5f9f9248c0fbad89a3edef5debe2b67be9f
parentbb6b273f3d8d38473cad7ce6f36c2c7b49e03ce6 (diff)
Improve doc of yield_self
* object.c: Add code samples for yield_self. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65271 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--object.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/object.c b/object.c
index e9639b0..949e521 100644
--- a/object.c
+++ b/object.c
@@ -570,6 +570,25 @@ rb_obj_size(VALUE self, VALUE args, VALUE obj)
* 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