Method: Enumerable#aT_all

Defined in:
lib/y_support/typing/enumerable/typing.rb

#aT_all(what_is_collection_element = nil, how_comply = nil, &b) ⇒ Object

Fails with TypeError unless all the members of the collection comply with the supplied block criterion. Optional arguments customize the error message. First optional argument describes the collection element, the second one describes the tested duck type. If the criterion block takes at least one argument, the receiver elemnts are passed to it (#all? method). If the criterion block takes no arguments (arity 0), it is gradually executed inside the elements (using #instance_exec). If no block is given, all members are required to be truey.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/y_support/typing/enumerable/typing.rb', line 11

def aT_all what_is_collection_element=nil, how_comply=nil, &b
  e = what_is_collection_element || "collection element"
  if block_given?
    m = "Each #{e} must %s!" %
      ( how_comply ? how_comply : "comply with the specification" )
    fail TypeError, m unless ( b.arity == 0 ?
                               all? { |e| e.instance_exec( &b ) } :
                               all? { |e| b.( e ) } )
  else
    m = "No #{e} must be nil or false!"
    fail TypeError, m unless all? { |e| e }
  end
  return self
end