Forum: Ruby-core [ruby-trunk - Feature #6721][Open] Object#yield_self

Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-07-11 09:39
(Received via mailing list)
Issue #6721 has been reported by alexeymuranov (Alexey Muranov).

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

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
Posted by Joshua Ballanco (jballanc)
on 2012-07-11 11:34
(Received via mailing list)
Issue #6721 has been updated by jballanc (Joshua Ballanco).


How is this significantly different than Object#tap?
----------------------------------------
Feature #6721: Object#yield_self
https://bugs.ruby-lang.org/issues/6721#change-27938

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
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-07-11 14:22
(Received via mailing list)
Issue #6721 has been updated by alexeymuranov (Alexey Muranov).


=begin
jballanc (Joshua Ballanco) wrote:
> How is this significantly different than Object#tap?

It executes the block and returns its output.  For example:

 puts "2*2 = #{ 2.yield_self { |x| x*x } }"
=end

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

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
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-07-14 17:11
(Received via mailing list)
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
Posted by Nobuyoshi Nakada (nobu)
on 2012-07-14 23:27
(Received via mailing list)
Issue #6721 has been updated by nobu (Nobuyoshi Nakada).


I'm not against the feature itself, but don't like the name.
----------------------------------------
Feature #6721: Object#yield_self
https://bugs.ruby-lang.org/issues/6721#change-28122

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
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-07-15 00:56
(Received via mailing list)
Issue #6721 has been updated by alexeymuranov (Alexey Muranov).


nobu (Nobuyoshi Nakada) wrote:
> I'm not against the feature itself, but don't like the name.

#yield_to, #submit_to, #surrender, #capitulate ? :)

Or otherwise, #apply:

2.apply { |x| x*x }  # => 4
----------------------------------------
Feature #6721: Object#yield_self
https://bugs.ruby-lang.org/issues/6721#change-28123

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
Posted by drbrain (Eric Hodel) (Guest)
on 2012-07-15 02:09
(Received via mailing list)
Issue #6721 has been updated by drbrain (Eric Hodel).


Your current names are less clear than using a local variable.  Using a 
local variable reveals your intentions very clearly:

  o = object.associated_object
  attr = o.attribute if o

It's obvious that attr is only set if the associated object exists.

For your second example there's just too much going on to clearly see 
what the intention is.  By first separating data gathering from creating 
of the mailing_address Hash things become much clearer:

  address = person[:address]
  street = address[:street] if address.is_a?(Hash)

  mailing_address = {
    :name   => person[:name],
    :street => street,
  }

As in the first example, your current names don't reveal what yield_self 
is supposed to do in a way that's clearer than using local variables for 
construction of mailing_address
----------------------------------------
Feature #6721: Object#yield_self
https://bugs.ruby-lang.org/issues/6721#change-28124

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
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-07-15 22:11
(Received via mailing list)
Issue #6721 has been updated by alexeymuranov (Alexey Muranov).


drbrain (Eric Hodel) wrote:
> Your current names are less clear than using a local variable.  Using a local 
variable reveals your intentions very clearly:

Well, using method chains with blocks is always less clear than using 
local variables.
----------------------------------------
Feature #6721: Object#yield_self
https://bugs.ruby-lang.org/issues/6721#change-28134

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
Posted by Thomas Sawyer (7rans)
on 2012-08-06 23:15
(Received via mailing list)
Issue #6721 has been updated by trans (Thomas Sawyer).


This is basically #ergo in Ruby Facets. Essentially:

  def ergo
    return yield(self) if block_given?
    self
  end

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

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
Posted by yhara (Yutaka HARA) (Guest)
on 2012-10-27 17:20
(Received via mailing list)
Issue #6721 has been updated by yhara (Yutaka HARA).

Category set to core
Target version set to next minor


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

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
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2012-11-10 11:20
(Received via mailing list)
Issue #6721 has been updated by alexeymuranov (Alexey Muranov).


=begin
After commenting on #6284, i have a new proposition for this method's 
name: (({Object#^})).  Also, i suggest to allow it to take a block, a 
proc, a lambda, or a symbol.  I think this will not conflict with 
existing uses of #^, however the classes that implement it for certain 
argument types should not forget to call (({super})) if the argument 
type is not recognized by them.

For example:

  # Formatting a string:
  format_as_title = lambda { |str| "Title: #{ str.strip.capitalize }" }
  title = " something to be a title " ^ format_as_title     # instead of 
`format_as_title.call(" something to be a title  ")`

  # Squareing the 2:
  four = 2 ^ { |x| x*x }                                    # instead of 
`four = 2**2`

  # Converting a string to an integer:
  five = "5" ^ :to_i                                        # instead of 
`five = "5".to_i`

This is consistent with a rare mathematical notation for function 
application: sometimes instead of  "f(x)",  the "exponential" notation 
"x^f" is used.

This would also open a door to compose lambdas from left to right, if 
the majority decides so (this is being discussed in #6284)
=end

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

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
Posted by boris_stitnicky (Boris Stitnicky) (Guest)
on 2012-11-18 04:21
(Received via mailing list)
Issue #6721 has been updated by boris_stitnicky (Boris Stitnicky).


#ergo is a well-thought method name, I like it better than all others.
----------------------------------------
Feature #6721: Object#yield_self
https://bugs.ruby-lang.org/issues/6721#change-33040

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
Posted by Charles Nutter (headius)
on 2012-11-24 18:29
(Received via mailing list)
Issue #6721 has been updated by headius (Charles Nutter).


It occurs to me #apply is used in some other languages to refer to the 
elements of a collection rather than to the collection itself.

[1,2,3].apply {|n| puts n}

Did we ever decide if the #self method would be added? If it were, it 
would be simple to have it take a block:

four = 2.self {|n| n * n}

That would make #self basically be #ergo as defined by Facets.

Worth noting that you can get nearly as concise syntax today, albeit in 
reverse order:

four = ->{|n| n * n}.(2)
----------------------------------------
Feature #6721: Object#yield_self
https://bugs.ruby-lang.org/issues/6721#change-33823

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
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2013-02-08 14:52
(Received via mailing list)
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
Posted by Alex aka Sinm (Guest)
on 2013-02-08 21:11
(Received via mailing list)
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
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2013-02-09 12:43
(Received via mailing list)
Issue #6721 has been updated by alexeymuranov (Alexey Muranov).


Anonymous wrote:
> 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

I can, but i guess i want it to look more like declarative programming, 
than like imperative.
----------------------------------------
Feature #6721: Object#yield_self
https://bugs.ruby-lang.org/issues/6721#change-36088

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
Posted by ko1 (Koichi Sasada) (Guest)
on 2013-02-18 01:35
(Received via mailing list)
Issue #6721 has been updated by ko1 (Koichi Sasada).

Assignee set to matz (Yukihiro Matsumoto)


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

Author: alexeymuranov (Alexey Muranov)
Status: Open
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
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
Posted by aleph1 (Elias Levy) (Guest)
on 2013-05-17 10:01
(Received via mailing list)
Issue #6721 has been updated by aleph1 (Elias Levy).


nobu (Nobuyoshi Nakada) wrote:
> I'm not against the feature itself, but don't like the name.

At its core this feature relates to method chaining and transforming the 
object, something that cannot be done with Object#tap.

Some suggested names then:  transform, alter, mutate, map, morph.

map may be the best choice, as its already used in enumerables and this 
is a natural equivalent for single objects.  That said, it may lead to 
unnoticed bugs if someone thinks they are applying a map operation on an 
enumerable but for some reason they do so against some other object.  So 
maybe one of the other names is better to ensure such cases fail.


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

Author: alexeymuranov (Alexey Muranov)
Status: Open
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
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
Posted by Nobuyoshi Nakada (nobu)
on 2013-05-17 10:48
(Received via mailing list)
(13/05/17 17:01), aleph1 (Elias Levy) wrote:
> map may be the best choice, as its already used in enumerables and this is a 
natural equivalent for single objects.  That said, it may lead to unnoticed bugs 
if someone thinks they are applying a map operation on an enumerable but for some 
reason they do so against some other object.  So maybe one of the other names is 
better to ensure such cases fail.

If it were Kernel#map, which would you expect by `{foo: 42}.map {...}` ?
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2013-05-17 18:33
(Received via mailing list)
Issue #6721 has been updated by alexeymuranov (Alexey Muranov).


aleph1 (Elias Levy) wrote:
> nobu (Nobuyoshi Nakada) wrote:
> > I'm not against the feature itself, but don't like the name.
>
> At its core this feature relates to method chaining and transforming the object, 
something that cannot be done with Object#tap.

I do not agree: it simply yield self to a proc.  It is more like a 
counterpart to `public_send`.  I will try to see if Haskel has it, and 
what's the name, or i will think if it corresponds well to some 
mathematical operation.
----------------------------------------
Feature #6721: Object#yield_self
https://bugs.ruby-lang.org/issues/6721#change-39375

Author: alexeymuranov (Alexey Muranov)
Status: Open
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
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
Posted by boris_stitnicky (Boris Stitnicky) (Guest)
on 2013-05-17 22:42
(Received via mailing list)
Issue #6721 has been updated by boris_stitnicky (Boris Stitnicky).


nobu (Nobuyoshi Nakada) wrote:
> I'm not against the feature itself, but don't like the name.
+1 to this opinion
----------------------------------------
Feature #6721: Object#yield_self
https://bugs.ruby-lang.org/issues/6721#change-39386

Author: alexeymuranov (Alexey Muranov)
Status: Open
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
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
Posted by alexeymuranov (Alexey Muranov) (Guest)
on 2013-05-18 10:23
(Received via mailing list)
Issue #6721 has been updated by alexeymuranov (Alexey Muranov).


=begin
It seems that Haskell does not have it: 
http://stackoverflow.com/questions/4090168/is-ther...

I have found that in the context of Church numerals in lambda calculus, 
the expression (({λm.λn.nm})) is called (({exp})): 
http://en.wikipedia.org/wiki/Church_encoding#Compu... 
.
One more reason to use the (({^})) operator as i have suggested :).

This operation also would roughly correspond to (({apply})) in Scheme 
with reverse order of arguments.  If i understand correctly, the 
(({apply})) in Scheme roughly correspond to (({call})) in Ruby, so maybe 
(({reverse_call}))?
=end

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

Author: alexeymuranov (Alexey Muranov)
Status: Open
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.