Issue #6373 has been updated by Andrew Vit.


<blockquote>
Mmm, no, that's quite different. "".presence # => nil for example.
</blockquote>

Yes, I was aware of the differences from ActiveSupport presence, I just wanted to point out the similar need for chaining "itself" with a block.

The proposed "itself" method also looks similar to "Enumerable#map" but for a single object, effectively:

<pre>
    ["ruby"].map {|n| n.upcase }.first
    # same as:
    "ruby".itself {|n| n.upcase }
</pre>

But I suppose "map" as a method name would be out of the question. 

Has anyone considered "yield" as the method name? It also seems to fit well, and would likely not conflict with anything:

<pre>
    class Object
      def yield
        if block_given?
          yield self
        else
          self
        end
      end
    end

    # yield as a noun means "the result": an object's result (its yield) is itself
    "ruby".yield  #=> "ruby"
    
    # yield as a verb means "give way to" or "produce": the object gives way to the block
    "ruby".yield {|s| s.upcase } #=> "RUBY"
</pre>

Although there might be confusion between yield as a keyword and self.yield as a method, I do like this symmetry:

<pre>
    yield self         # calls block with self, returns result
    self.yield(&block) # calls block with self, returns result
    self.yield         # implicit identity, just like: {|obj| obj }
<pre>


----------------------------------------
Feature #6373: public #self
https://bugs.ruby-lang.org/issues/6373#change-44578

* Author: Thomas Sawyer
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* Category: core
* Target version: next minor
----------------------------------------
=begin
This was recently suggested to me as an extension:

  class Object
    # An identity method that provides access to an object's 'self'.
    #
    # Example:
    #   [1,2,3,4,5,1,2,2,3].group_by(&:identity)
    #   #=> {1=>[1, 1], 2=>[2, 2, 2], 3=>[3, 3], 4=>[4], 5=>[5]}
    #
    def identity
      self
    end
  end

First, is such a method commonly useful enough to warrant existence?

Second, it makes me wonder if #self should be a public method in general.
=end

---Files--------------------------------
self.pdf (77.8 KB)


-- 
http://bugs.ruby-lang.org/