From: brandon_weaver@... Date: 2014-10-01T00:13:50+00:00 Subject: [ruby-core:65332] [ruby-trunk - Feature #10095] Object#as Issue #10095 has been updated by Brandon Weaver. Andrew Vit wrote: > 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" > ~~~ related: #10308 I've actually done it too before, I just literally named it pipe: https://github.com/baweaver/pipeable or Stream which was something to the same idea: https://github.com/baweaver/streamable Either way this has been hacked to death in multiple places. I'd think that just going for an Elixir like pipe would be the cleanest implementation honestly. ---------------------------------------- Feature #10095: Object#as https://bugs.ruby-lang.org/issues/10095#change-49146 * 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/