如何通过一系列代码块传递索引?
# i'm not sure how to set this up
def call(index=0, &block) # index here is likely not needed
yield (index+1) # or block.call(index)
end
call{call{call{}}}
应该给出总计数(3)和每次通话的计数 最好不必明确使用call {| i |打电话给{| i | }}
答案 0 :(得分:2)
尝试此变体:
def call(index = 0)
if block_given? and (res = yield(index + 1)) != nil
res + 1
else
index + 1
end
end