Created
September 19, 2012 21:09
-
-
Save gnarmis/3752271 to your computer and use it in GitHub Desktop.
Piping arguments through multiple functions 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
# piping example in Ruby | |
def foo(data) | |
data[:a] += 1 | |
data | |
end | |
def bar(data) | |
data[:b] += 10 | |
data | |
end | |
def pipe args, *methods | |
methods.reduce(args) { |a, m| send(m, a) } | |
end | |
hash = {:a => 0, :b => 0} | |
pipe hash, :foo, :bar | |
#=> {:a=>1, :b=>10} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment