Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created June 29, 2021 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/5a55cfc61ea57f9157280f6e053b7b5b to your computer and use it in GitHub Desktop.
Save JoshCheek/5a55cfc61ea57f9157280f6e053b7b5b to your computer and use it in GitHub Desktop.
Ruby pipeline, based on JS example
# https://babeljs.io/blog/2018/07/19/whats-happening-with-the-pipeline-proposal
module Pipeable
def |(fn)
fn.to_proc.call self
end
refine(Enumerable) { include Pipeable }
refine(Proc) { include Pipeable }
refine(Array) { include Pipeable }
refine Kernel do
def array_map(&b)
-> arr { arr.map &b }
end
def array_filter(&b)
-> arr { arr.select &b }
end
def count
:count.to_proc
end
end
end
using Pipeable
(1..10) | # => 1..10
array_map { |x| x * x } | # => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
array_filter(&:even?) | # => [4, 16, 36, 64, 100]
:sum # => 220
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment