One step closer to a good pipeline operator for Ruby
I’ve previously yearned for something like Elm and Elixir’s |>
operator in Ruby. Turns out, this clever bit of concision is in Ruby 2.5:
object.yield_self {|x| block } → an_object # Yields self to the block and returns the result of the block. class Object def yield_self yield(self) end end
I would prefer then
or even |
to the verbosely literal yield_self
, but I’ll take anything. Surprisingly, both of my options are legal method names!
class Object def then yield self end def | yield self end end require "pathname" __FILE__. then { |s| Pathname.new(s) }. yield_self { |p| p.read }. | { |source| source.each_line }. select { |line| line.match /^\W*def ([\S]*)/ }. map { |defn| p defn }
However, |
already has 20+ implementations, either of the mathematical logical-OR variety or of the shell piping variety. Given the latter, maybe there’s a chance!
Next, all we need is:
- a syntax to curry a method by name (which is in the works!)
- a syntax to partially apply said curry
If those two things make their way into Ruby, I can move on to my next pet feature request: a module/non-global namespace scheme ala Python, ES6, Elixir, etc. A guy can dream!