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/