0

How would I pass an index through a series of code blocks?

# 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{}}}

should give a total count (3) and the count at each call preferably without having to explicitly use call { |i| call{ |i| } }

4

1 回答 1

2

试试这个变种:

def call(index = 0)
  if block_given? and (res = yield(index + 1)) != nil
    res + 1
  else
    index + 1
  end
end
于 2013-06-05T10:56:51.577 回答