Created
July 3, 2014 05:58
-
-
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
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
#!/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