RubyFlow The Ruby and Rails community linklog

Elixir/Unix style pipe operations in Ruby

This proof of concept demonstrates a concise/elegant syntax for pipelining and method composition!


-9.pipe { abs | Math.sqrt | to_i } #=> 3

[9, 64].map(&Math.pipe.sqrt)           #=> [3.0, 8.0]
[9, 64].map(&Math.pipe.sqrt.to_i.to_s) #=> ["3", "8"]

"https://api.github.com/repos/ruby/ruby".pipe do
  URI.parse
  Net::HTTP.get
  JSON.parse.fetch("stargazers_count")
  yield_self { |n| "Ruby has #{n} stars" }
  Kernel.puts
end
#=> Ruby has 15120 stars

There’s been some recent discussions in the Ruby tracker related to method referencing and composition. The pipe_operator gem was created to propose an alternative syntax for this kind of behavior:

Is this something that you’d like to see in Ruby core? Please share your opinions!

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in