Elixir style pipe operator for Ruby
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
bin
lib
test
.gitignore
.travis.yml
Gemfile
LICENSE.txt
README.md
Rakefile
pipe_envy.gemspec

README.md

Lines of Code Code Status Dependency Status Build Status Coverage Status Downloads

Sponsor

PipeEnvy

WARNING

This lib is experimental & is probably not a good idea.

PipeEnvy overrides the pipe operator | on Array & Integer. It also adds it to Object.

Fun Stuff

Elixir's pipe operator is very cool & supports intuitive reasoning about data transformations similar to Unix pipelines.

"Elixir Rocks" |> String.upcase |> String.split # => ["ELIXIR", "ROCKS"]

Rubyists can now enjoy this same mental model of data transformation.

gem install pipe_envy
require "pipe_envy"

# refinements that apply extensions to Object, Array, & Integer in the current scope
using PipeEnvy

"Ruby Rocks" | :upcase | :split # => ["RUBY", "ROCKS"]

Here's a more sophisticated albeit contrived example. Notice that methods which require arguments are piped as Arrays.

magic = (1..100) \
  | :to_a \
  | [:select!, -> (i) { i.even? }] \
  | [:map, -> (i) { i ** 10 }] \
  | :sum \
  | Math.method(:sqrt) \
  | :to_s \
  | :chars \
  | :reverse \
  | [:[], 3] \
  | :to_i

# => 7

Be sure to check out Chainable Methods which offers similar behavior.

Here's another example similar to the one on the chainable_methods repo.

magic = "foo bar http://github.com/hopsoft/pipe_envy foo bar" \
  | URI.method(:extract) \
  | :first \
  | URI.method(:parse) \
  | :open \
  | :readlines \
  | :join \
  | Nokogiri::HTML.method(:parse) \
  | [:css, "h1"] \
  | :first \
  | :text \
  | :strip

# => "hopsoft/pipe_envy"