Created
October 12, 2019 22:12
-
-
Save krisleech/71b2c23fc696ab38b89bcf9165d19729 to your computer and use it in GitHub Desktop.
Piping in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'pry' | |
| class Pipe | |
| def self.pipe(*args) | |
| new(args) | |
| end | |
| def self.[](*args) | |
| new(args) | |
| end | |
| def initialize(args) | |
| @args = args | |
| end | |
| def to_proc | |
| Proc.new do |input| | |
| @args.reduce(input) do |memo, arg| | |
| memo.send(arg) | |
| end | |
| end | |
| end | |
| end | |
| ⏩ = Pipe | |
| res = %w(1 2 3 4).map(&⏩[:to_i, :succ]) | |
| puts res.inspect # => [2,3,4,5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment