Project

General

Profile

Actions

Feature #7388

closed

Object#embed

Added by zzak (zzak _) about 12 years ago. Updated about 12 years ago.

Status:
Rejected
Target version:
[ruby-core:49536]

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


Files

object_embed.patch (1.74 KB) object_embed.patch zzak (zzak _), 11/19/2012 07:40 AM

Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #6721: Object#yield_selfClosedmatz (Yukihiro Matsumoto)Actions
Has duplicate Ruby master - Feature #12760: Optional block argument for `itself`ClosedActions

Updated by mame (Yusuke Endoh) about 12 years ago

  • Status changed from Open to Assigned
  • Assignee changed from mame (Yusuke Endoh) to matz (Yukihiro Matsumoto)
  • Target version set to 2.6

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

My personal opinion: the method name will matter.

--
Yusuke Endoh

Updated by marcandre (Marc-Andre Lafortune) about 12 years 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.

Updated by nathan.f77 (Nathan Broadbent) about 12 years 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}}

Updated by trans (Thomas Sawyer) about 12 years ago

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

Updated by matz (Yukihiro Matsumoto) about 12 years 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

Updated by trans (Thomas Sawyer) about 12 years 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

Actions #7

Updated by nobu (Nobuyoshi Nakada) about 8 years ago

  • Has duplicate Feature #12760: Optional block argument for `itself` added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0