Skip to content

Instantly share code, notes, and snippets.

@gnarmis
Created September 19, 2012 21:09
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 gnarmis/3752271 to your computer and use it in GitHub Desktop.
Save gnarmis/3752271 to your computer and use it in GitHub Desktop.
Piping arguments through multiple functions in Ruby
# 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