Feature #6721

Object#yield_self

Added by Alexey Muranov almost 2 years ago. Updated 7 months ago.

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

Description

=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


Related issues

Related to ruby-trunk - Feature #7388: Object#embed Rejected 11/19/2012

History

#1 Updated by Joshua Ballanco almost 2 years ago

How is this significantly different than Object#tap?

#2 Updated by Alexey Muranov almost 2 years ago

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

2.yield_self { |x| x*x } # => 4

#3 Updated by Alexey Muranov almost 2 years ago

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.attribute unless o.nil? }

mailing_address = { :name   => person[:name],
                    :street => person[:address].yield_self { |a| a[:street] if a.is_a?(Hash) }
                  }

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

#4 Updated by Nobuyoshi Nakada almost 2 years ago

I'm not against the feature itself, but don't like the name.

#5 Updated by Alexey Muranov almost 2 years ago

nobu (Nobuyoshi Nakada) wrote:

I'm not against the feature itself, but don't like the name.

#yieldto, #submitto, #surrender, #capitulate ? :)

Or otherwise, #apply:

2.apply { |x| x*x }  # => 4

#6 Updated by Eric Hodel almost 2 years ago

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 yieldself is supposed to do in a way that's clearer than using local variables for construction of mailingaddress

#7 Updated by Alexey Muranov almost 2 years ago

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, i think.

#8 Updated by Thomas Sawyer over 1 year ago

This is basically #ergo in Ruby Facets. Essentially:

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

#9 Updated by Yutaka HARA over 1 year ago

  • Category set to core
  • Target version set to next minor

#10 Updated by Alexey Muranov over 1 year ago

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["something to be a title"]`

# Squaring 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 "xf" 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)

#11 Updated by Boris Stitnicky over 1 year ago

#ergo is a well-thought method name, I like it better than all others.

#12 Updated by Charles Nutter over 1 year ago

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)

#13 Updated by Alexey Muranov about 1 year ago

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
provided the affiliation or the position. So i did like this:

class Participant
  def full_name_with_affiliation_and_position
    full_name +
      lambda { |x| x.empty? ? '' : " (#{ x })" }[[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 { |x| x.empty? ? '' : " (#{ x })" }
  end
end

This would be a bit more readable for me.

Edited 2013-02-09.

#14 Updated by Anonymous about 1 year ago

Why you can't simply do the following?

def fullnamewithaffiliationandposition
a
ap = " (#{aap})" unless (aap = [affiliation, academicposition].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 fullnamewithaffiliationandposition
full
name +
lambda { |aap| aap.empty? ? '' : " (#{ aap })" }[[affiliation, academicposition].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 fullnamewithaffiliationandposition
full
name +
[affiliation, academicposition].compact.join(', ')].yieldself { |aap| aap.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/

#15 Updated by Alexey Muranov about 1 year ago

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.

#16 Updated by Koichi Sasada about 1 year ago

  • Assignee set to Yukihiro Matsumoto

#17 Updated by Elias Levy 11 months ago

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.

#18 Updated by Nobuyoshi Nakada 11 months ago

(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 {...} ?

#19 Updated by Boris Stitnicky 11 months ago

nobu (Nobuyoshi Nakada) wrote:

I'm not against the feature itself, but don't like the name.
+1 to this opinion

#20 Updated by Alexey Muranov 11 months ago

I have checked if by any chance Haskell had it, apparently it doesn't: ((URL:http://stackoverflow.com/questions/4090168/is-there-an-inverse-of-the-haskell-operator))

I have found that in Alonzo Church's "The calculi of lambda-conversion", he uses "[MN]" (with superscript) as an alternative notation for the "application" of a term N to a term M (in addition to the basic lambda-calculus notation "(NM)").

This operation would roughly correspond to apply in Scheme with reverse order of arguments. If i understand correctly, the apply in Scheme roughly corresponds to call in Ruby, so maybe reverse_call?

Edited 2013-08-31.

#21 Updated by Alexey Muranov 8 months ago

Another idea for the name: Object#cast.

class Object
  def cast(*args)
    if block_given?
      yield(self, *args)
    else
      p = args.pop
      unless p.respond_to?(:call)
        raise ArgumentError, 'the last argument should be callable when no block is given'
      end
      p.call(self, *args)
    end
  end
end

2.cast {|x| x**3 } # => 8

p = proc {|x, y| x**y }
3.cast(2, p) # => 9

Another option: Object#toss.

Edited 2013-09-01.

#22 Updated by Abinoam P. Marques Jr. 7 months ago

=begin
May I give a name suggestion?
Does "tap!" make sense in english?

(({2.tap {|x| x*2 }})) ((|# => 2|))

(({2.tap! {|x| x*2 }})) ((|# => 4|))

The exclamation mark alerts that the return value is being changed.

=end

Also available in: Atom PDF