Skip to content

Instantly share code, notes, and snippets.

@dcorking
Created July 3, 2014 05:58
Show Gist options
  • Save dcorking/a7422c654d28394b4864 to your computer and use it in GitHub Desktop.
Save dcorking/a7422c654d28394b4864 to your computer and use it in GitHub Desktop.
A ruby method can accept two blocks as arguments, if we convert them to procs or lambdas
#!/bin/ruby
# Inspired by http://innig.net/software/ruby/closures-in-ruby
# Can this take 2 blocks?
# def apply_two_things(&block1, arg_after_block)
# end
# syntax error, unexpected ',', expecting ')'
# What about 2 procs?
def apply_two_things(first, second, arg)
second.call(first.call(arg))
end
double = proc {|x| x * 2}
plus3 = lambda{|x| x + 3}
calculated = apply_two_things(double, plus3, 5)
# returns 13
# Success !!!
puts "5 plus 3 times 2 is #{calculated}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment