Elixir style pipe operator for Ruby
Switch branches/tags
Nothing to show
Clone or download
Latest commit fbe0326 Aug 2, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin Initial commit Jul 28, 2017
lib Get rid of global Jul 29, 2017
test Added chainable methods example Jul 29, 2017
.gitignore Initial commit Jul 28, 2017
.travis.yml Initial commit Jul 28, 2017
Gemfile Initial commit Jul 28, 2017
LICENSE.txt Initial commit Jul 28, 2017
README.md Repos sponsor Aug 2, 2017
Rakefile Initial commit Jul 28, 2017
pipe_envy.gemspec Added chainable methods example Jul 29, 2017

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"