Feature #7388

Object#embed

Added by Zachary Scott over 1 year ago. Updated over 1 year ago.

[ruby-core:49536]
Status:Rejected
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:core
Target version:next minor

Description

=begin
From github:
https://github.com/ruby/ruby/pull/67

Add Object#embed which works like tap but returns the block's value

This avoids breaking from method chains in many cases. I don't see any other way to do this without this method.

Can someone confirm pull request are a valid way of submitting patches ? I cannot register on the mailing list for some reason, it seems to be broken.

Example:

(({[ 1, 2, 3, 4].select{|x| x.odd?}.embed{|x| {:total => x.count, :data => x}}}))
=end

object_embed.patch Magnifier (1.74 KB) Zachary Scott, 11/19/2012 07:40 AM


Related issues

Related to ruby-trunk - Feature #6721: Object#yield_self Open 07/11/2012

History

#1 Updated by Yusuke Endoh over 1 year ago

  • Status changed from Open to Assigned
  • Assignee changed from Yusuke Endoh to Yukihiro Matsumoto
  • Target version set to next minor

In principle, I have no right to judge a feature request.
Please ask matz.

My personal opinion: the method name will matter.

Yusuke Endoh mame@tsg.ne.jp

#2 Updated by Marc-Andre Lafortune over 1 year ago

mame (Yusuke Endoh) wrote:

My personal opinion: the method name will matter.

I'd even say that embed is wrong.

I would like to know of a good example of use case. I often succumb to the temptation of writing more complex code to avoid creating a local var, but that doesn't always make for better code.

The example given in the github PR was:

[ 1, 2, 3, 4].select{|x| x.odd?}.embed{|x| {:total => x.count, :data => x}}...

I feel the following is clearer:

odd_nbs = [ 1, 2, 3, 4].select{|x| x.odd?}
{:total => odd_nbs.count, :data => odd_nbs}...

Until I see a good use case (and a good method name is found), I'm -1 for this.

#3 Updated by Nathan Broadbent over 1 year ago

I'd even say that embed is wrong.

I would like to know of a good example of use case. I often succumb to the
temptation of writing more complex code to avoid creating a local var, but
that doesn't always make for better code.

The example given in the github PR was:

[ 1, 2, 3, 4].select{|x| x.odd?}.embed{|x| {:total => x.count, :data

=> x}}...

I feel the following is clearer:

odd_nbs = [ 1, 2, 3, 4].select{|x| x.odd?}
{:total => odd_nbs.count, :data => odd_nbs}...

How about infix? I think this method could be a useful addition, even if
it's rarely used. Also, the example usage might be clearer with a better
block argument:

  [ 1, 2, 3, 4].select{|x| x.odd?}.infix{|odd_nbs| {:total => odd_nbs.count,

:data => odd_nbs}}

#4 Updated by Thomas Sawyer over 1 year ago

Is this #ergo? Also see #6721 and #6373.

#5 Updated by Yukihiro Matsumoto over 1 year ago

  • Status changed from Assigned to Rejected

=begin
I am against those names (not the feature).

: embed
the term 'embed' has different impression.

: infix
'infix' reminds me 'infix operators'.

: ergo
Latin? I am strongly against introducing non-popular non-English names as standard methods.

Basically it's too much for a method that can be defined in 3 lines in your code.
Come back if your think of another name.

Matz.

=end

#6 Updated by Thomas Sawyer over 1 year ago

=begin
I thought about a few terms that might work in both cases, i.e. the so called "embed" case of this issue and the "identity" case of #6373. All of the obvious plays on the term "self" didn't fair too well. So I took a step back and asked what it was the method was really asking for. I answered, "a reference to the object itself". So then, it occurred to me, what about #reference?

a.group_by(&:reference)

a.reference{ |r| ... }

=end

Also available in: Atom PDF