我有一个方法可以接受一个块,让我们把它叫做外层。它又调用一个接受另一个块的方法,称它为内部。在Ruby中访问传递的块
我想要发生的是外层调用内层,传递一个新的块调用第一个块。
这里有一个具体的例子:
class Array
def delete_if_index
self.each_with_index { |element, i| ** A function that removes the element from the array if the block passed to delete_if_index is true }
end
end
['a','b','c','d'].delete_if_index { |i| i.even? }
=> ['b','d']
传递给delete_if_index块被传递到each_with_index块调用。
这是可能的,在Ruby中,更广泛地说,我们有多少访问接收它的函数内的块?
我想我知道你的意思,但是如果你能显示出一些你想做的事情的伪代码可能会有所帮助。当你谈论接收块时,你的意思是像Ruby语言结构中的块一样,还是你的意思是闭包? – glenatron
您可以将块包装在另一个块中。那对你有用吗? –
感谢您的评论,我添加了一个具体的例子。我怀疑我可能会对此有点错误,但知道这件事仍然很有趣。 – superluminary