Why you can't simply do the following? def full_name_with_affiliation_and_position a_ap = " (#{a_ap})" unless (a_ap = [affiliation, academic_position].compact.join ', ').empty? "#{full_name}#{a_ap}" end On Friday, 8 February 2013 г. at 17:41, alexeymuranov (Alexey Muranov) wrote: > > Issue #6721 has been updated by alexeymuranov (Alexey Muranov). > > > =begin > Here is a "real life" use case. It again has to do with formatting strings. > > I want to have a list of conference participants in the form: > Full Name (Affiliation, academic position) > but without empty parentheses or trailing comma if the person has not > profided the affiliation or the position. So i did like this: > > class Participant > def full_name_with_affiliation_and_position > full_name + > lambda { |a_ap| a_ap.empty? ? '' : " (#{ a_ap })" }[[affiliation, academic_position].compact.join(', ')] > end > end > > (I will appreciate any more elegant solution.) > > With (({#yield_self})) (or any other name for it), i would have written: > > class Participant > def full_name_with_affiliation_and_position > full_name + > [affiliation, academic_position].compact.join(', ')].yield_self { |a_ap| a_ap.empty? ? '' : " (#{ a_ap })" } > end > end > > This would be a bit more readable for me. > =end > > ---------------------------------------- > Feature #6721: Object#yield_self > https://bugs.ruby-lang.org/issues/6721#change-36056 > > Author: alexeymuranov (Alexey Muranov) > Status: Open > Priority: Normal > Assignee: > Category: core > Target version: next minor > > > =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/ > >