Skip to content

Instantly share code, notes, and snippets.

@cjavdev
Created July 17, 2013 03:56
Show Gist options
  • Save cjavdev/6017577 to your computer and use it in GitHub Desktop.
Save cjavdev/6017577 to your computer and use it in GitHub Desktop.
Ruby my_curry_sum demonstrating closures.
def my_curry_sum x # x= number of times to curry
sum = 0
# this will not work with a proc as a proc will return
# out of the my_curry_sum method
sum_prc = lambda do |n|
sum += n
x -= 1
return sum if x == 0
return sum_prc
end
return sum_prc
end
sum = my_curry_sum(3)
p sum[1][2][3] # this is a fancy way of doing sum.call(1).call(2).call(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment