[Ruby] Yield but no block argument

Such a function


module Enumerable
  def all?(*several_variants)
    yield to_enum.next; __unknown

use all? like this


%w[ant bear cat].all? { |word| word.length >= 3 } #=> true

Why can I pass it even though there is no & block in the argument? That's because the block argument can be omitted if there is a yield in the ** method **.

However, if you do not look inside the method, you will not know if you can pass the block, so it seems that you need to consider whether to omit it.

Recommended Posts

[Ruby] Yield but no block argument
[Ruby] Block
[ruby] Method call with argument