ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "alexeymuranov (Alexey Muranov)" <redmine@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:46479] [ruby-trunk - Feature #6721] Object#yield_self
Date: Sun, 15 Jul 2012 00:08:12 +0900	[thread overview]
Message-ID: <redmine.journal-28120.20120715000809@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-6721.20120711163521@ruby-lang.org


Issue #6721 has been updated by alexeymuranov (Alexey Muranov).


=begin
I've come up with some use case for illustration.  I have also looked into the Ruby on Rails (({Object#try})) method because it can serve a similar purpose.  I think (({yield_self})) is more basic than (({try})).

Here are two examples of a use case:

 attr = object.associated_object.yield_self { |o| o.nil? ? nil : o.attribute }

 mailing_address = { :name   => person[:name],
                     :street => person[:address].yield_self { |a| a.is_a?(Hash) ? a[:street] : nil }
                   }

Here is for comparison the implementation of (({Object#try})) in Ruby on Rails:

 def try(*a, &b)
   if a.empty? && block_given?
     yield self
   else
     __send__(*a, &b)
   end
 end
=end

----------------------------------------
Feature #6721: Object#yield_self
https://bugs.ruby-lang.org/issues/6721#change-28120

Author: alexeymuranov (Alexey Muranov)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


=begin
I think the following method is missing from Ruby:

 class Object
   def yield_self(*args)
     yield(self, *args)
   end
 end

I do not know a good use case, but it looks very natural to me.  It can be used in method chains.

What do you think?  Is there an alternative?
=end



-- 
http://bugs.ruby-lang.org/

  parent reply	other threads:[~2012-07-14 15:13 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-11  7:35 [ruby-core:46320] [ruby-trunk - Feature #6721][Open] Object#yield_self alexeymuranov (Alexey Muranov)
2012-07-11  9:33 ` [ruby-core:46321] [ruby-trunk - Feature #6721] Object#yield_self jballanc (Joshua Ballanco)
2012-07-11 12:22 ` [ruby-core:46323] " alexeymuranov (Alexey Muranov)
2012-07-14 15:08 ` alexeymuranov (Alexey Muranov) [this message]
2012-07-14 21:27 ` [ruby-core:46481] " nobu (Nobuyoshi Nakada)
2012-07-14 22:54 ` [ruby-core:46483] " alexeymuranov (Alexey Muranov)
2012-07-15  0:08 ` [ruby-core:46484] " drbrain (Eric Hodel)
2012-07-15 20:10 ` [ruby-core:46498] " alexeymuranov (Alexey Muranov)
2012-08-06 21:15 ` [ruby-core:47027] " trans (Thomas Sawyer)
2012-10-27 15:18 ` [ruby-core:48490] " yhara (Yutaka HARA)
2012-11-10 10:19 ` [ruby-core:49192] " alexeymuranov (Alexey Muranov)
2012-11-18  3:20 ` [ruby-core:49508] " boris_stitnicky (Boris Stitnicky)
2012-11-24 17:28 ` [ruby-core:50047] " headius (Charles Nutter)
2013-02-08 13:41 ` [ruby-core:52035] " alexeymuranov (Alexey Muranov)
2013-02-08 20:11   ` [ruby-core:52044] " Alex aka Sinm
2013-02-09 11:42 ` [ruby-core:52070] " alexeymuranov (Alexey Muranov)
2013-02-18  0:21 ` [ruby-core:52423] " ko1 (Koichi Sasada)
2013-05-17  8:01 ` [ruby-core:55022] " aleph1 (Elias Levy)
2013-05-17  8:48   ` [ruby-core:55023] " Nobuyoshi Nakada
2013-05-17 16:32 ` [ruby-core:55029] " alexeymuranov (Alexey Muranov)
2013-05-17 20:41 ` [ruby-core:55032] " boris_stitnicky (Boris Stitnicky)
2013-05-18  8:21 ` [ruby-core:55047] " alexeymuranov (Alexey Muranov)
2013-08-31 20:32 ` [ruby-core:56950] " alexeymuranov (Alexey Muranov)
2013-09-29 11:04 ` [ruby-core:57465] " abinoam (Abinoam P. Marques Jr.)
2015-11-20 18:13 ` [ruby-core:71612] [Ruby trunk " nobu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-28120.20120715000809@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).