[Ruby] [Ruby] Although there is yield, there is no block argument

less than 1 minute read

Function like this

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 doesn’t the argument have &block? 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 necessary to consider whether to omit it.

Tags:

Updated: