Module: Enumerable

Defined in:
lib/y_support/core_ext/enumerable/misc.rb,
lib/y_support/typing/enumerable/typing.rb

Instance Method Summary collapse

Instance Method Details

#all_kind_of?(kind) ⇒ Boolean

Checks whether #all? collection elements are #kind_of? module supplied as an argument

Returns:

  • (Boolean)


7
8
9
# File 'lib/y_support/core_ext/enumerable/misc.rb', line 7

def all_kind_of?( kind )
  all? {|e| e.kind_of? kind }
end

#all_numeric?Boolean

Checks whether #all? collection elements are #kind_of? Numeric.

Returns:

  • (Boolean)


13
14
15
# File 'lib/y_support/core_ext/enumerable/misc.rb', line 13

def all_numeric?
  all? {|e| e.kind_of? Numeric }
end

#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

#aT_all_comply(klass, what_is_collection_element = nil) ⇒ Object

Fails with TypeError unless all collection members declare compliance with the class supplied as an argument. Second optional argument (collection element description) customizes the error message.



41
42
43
44
45
46
# File 'lib/y_support/typing/enumerable/typing.rb', line 41

def aT_all_comply klass, what_is_collection_element=nil
  e = what_is_collection_element || "collection element"
  m = "Each #{e} must declare compliance to #{klass}!"
  fail TypeError, m unless all? { |e| e.class_complies? klass }
  return self
end

#aT_all_kind_of(klass, what_is_collection_element = nil) ⇒ Object

Fails with TypeError unless all collection members are #kind_of? the class supplied as argument. Second optional argument (collection element description) customizes the error message.



30
31
32
33
34
35
# File 'lib/y_support/typing/enumerable/typing.rb', line 30

def aT_all_kind_of klass, what_is_collection_element=nil
  e = what_is_collection_element || "collection element"
  m = "Each #{e} must be kind of #{klass}!"
  fail TypeError, m unless all? { |e| e.kind_of? klass }
  return self
end

#aT_all_numeric(what_is_collection_element = nil) ⇒ Object

Fails with TypeError unless all the collection members declare compliance with Numeric. Second optional argument (collection element description) customizes the error message.



52
53
54
55
56
57
# File 'lib/y_support/typing/enumerable/typing.rb', line 52

def aT_all_numeric what_is_collection_element=nil
  e = what_is_collection_element || "collection element"
  m = "Each #{e} must declare compliance with Numeric!"
  fail TypeError, m unless all? { |e| e.class_complies? Numeric }
  return self
end

#aT_subset_of(other_collection, what_is_receiver_collection = nil, what_is_other_collection = nil) ⇒ Object

Fails with TypeError unless all the collection members are included in the collection supplied as argument. Second optional argument (collection element description) customizes the error message.



63
64
65
66
67
68
69
70
71
72
# File 'lib/y_support/typing/enumerable/typing.rb', line 63

def aT_subset_of other_collection, what_is_receiver_collection=nil,
                 what_is_other_collection=nil
  rc = what_is_receiver_collection ?
    what_is_receiver_collection.to_s.capitalize : "collection"
  oc = what_is_other_collection ? what_is_other_collection.to_s.capitalize :
    "the specified collection"
  m = "The #{rc} must be a subset of #{oc}"
  fail TypeError, m unless all? { |e| other_collection.include? e }
  return self
end

#subset_of?(other_collection) ⇒ Boolean Also known as: ⊂?

Checks whether the receiver collection is fully included in the collection supplied as an argument.

Returns:

  • (Boolean)


20
21
22
# File 'lib/y_support/core_ext/enumerable/misc.rb', line 20

def subset_of?( other_collection )
  all? {|e| other_collection.include? e }
end

#superset_of?(other_collection) ⇒ Boolean Also known as: ⊃?

Checks whether the receiver collection contains every element of the collection supplied as an argument.

Returns:

  • (Boolean)


28
29
30
# File 'lib/y_support/core_ext/enumerable/misc.rb', line 28

def superset_of?( other_collection )
  other.all? {|e| self.include? e }
end