[Ruby] Yield but no block argument

Such a function

enumerable.rb


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

use all? like this

sample.rb


%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
About ruby block
[ruby] Method call with argument