Issue #10095 has been updated by Andrew Vit.


This feature also looks a lot like a pipeline.

Here are some examples how people implemented that before, either using simple blocks or more complex generator/consumer queues:

https://gist.github.com/pcreux/2f87847e5e4aad37db02
https://github.com/meh/ruby-thread#pipe
http://pragdave.me/blog/2007/12/30/pipelines-using-fibers-in-ruby-19/

For the purposes of this feature the method name could be something like `pipe_to`. (Hopefully that's not confusing with IO.pipe.)

Eventually, this could be an opportunity for pipelines as a first-class concept with better syntax sugar, similar to one of the examples above, or like Elixir (`|>`). Someone else can make a good proposal for that, but to think ahead maybe this method name should express "pipe" as part of its name.

Here is my own naive example:

~~~
class Object
  def | other
    case other
    when Proc
      if self.is_a?(Proc) 
        proc { |input| other.call( self.call(input) ) }
      else
        other.call(self)
      end
    else
      super
    end
  end
end

"ruby" |-> r { r.upcase }
#=> "RUBY"

pipeline = &:upcase | &:reverse
"ruby" | pipeline
#=> "YRUB"
~~~


----------------------------------------
Feature #10095: Object#as
https://bugs.ruby-lang.org/issues/10095#change-49106

* Author: Akira Matsuda
* Status: Open
* Priority: Normal
* Assignee: 
* Category: core
* Target version: current: 2.2.0
----------------------------------------
We've had so many times of feature requests for a method similar to Object#tap that doesn't return self but returns the given block's execution result (e.g. #7388, #6684, #6721 ).

I'm talking about something like this in Ruby of course:
Object.class_eval { def as() yield(self) end }

IIRC Matz is not against introducing this feature but he didn't like any of the names proposed in the past, such as embed, do, identity, ergo, reference, yield_self, itself, apply, map, tap!, etc.

So, let us propose a new name, Object#as today.
It's named from the aspect of the feature that it gives the receiver a new name "as" a block local variable.
For instance, the code reads so natural and intuitive like this:

(1 + 2 + 3 + 4).as {|x| x ** 2}
=> 100

Array.new.as {|a| a << 1; a << 2}
=> [1, 2]

---Files--------------------------------
itself-block.patch (1.35 KB)


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