Ruby 中的闭包 2018 程序啪啪啪
http://rubyer.me/blog/917/
Ruby:
- block = Proc
- Proc = code part 代码片断, 与引用部分属于同一个上下文
- lambda = noname Method
- Method
def a
sum = 0
return lambda do |x|
sum += x
end
end
pos = a
neg = a
(1.upto(5)).each do |i|
puts '%d %d' % [pos.call(i),neg.call(-2*i)]
end
# 1 -2
# 3 -6
# 6 -12
# 10 -20
# 15 -30
创建@
2014-09-03
最后修改@
2014-09-03