Project

pipe_envy

0.01
No commit activity in last 3 years
No release in over 3 years
Elixir style pipe operator for Ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

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"