我找不到它的名称,无法了解其工作原理和工作方式的更多信息,但我们发现您可以执行以下操作:
Person = Struct.new(:name)
people = [Person.new('foo'), Person.new('bar')]
# => [#<struct Person name="foo">, #<struct Person name="bar">]
people.find { |person| person.name == 'baz' }
# => nil
people.find(->{ [] }) { |person| person.name == 'baz' }
# => []
我想,每当有东西返回 nil 时,这就是一个失败的 block ,但也许有人可以阐明它是如何工作的?
最佳答案
来自documentation :
find(ifnone = nil) {| obj | block } → obj or nil
find(ifnone = nil) → an_enumerator
Passes each entry in enum to block. Returns the first for which block is not false. If no object matches, calls ifnone and returns its result when it is specified, or returns nil otherwise.
If no block is given, an enumerator is returned instead.
关于ruby - Ruby 可枚举的辅助 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22309045/