Class: Array

Inherits:
Object show all
Defined in:
lib/y_support/core_ext/array/misc.rb,
lib/y_support/typing/array/typing.rb

Instance Method Summary collapse

Instance Method Details

#aT_includes(element, what_is_element = nil) ⇒ Object Also known as: aT_include

This method takes a block and fails with TypeError, if the receiver array fails to include the specified element. An optional argument customizes the error message (element description).

Raises:



9
10
11
12
13
14
15
# File 'lib/y_support/typing/array/typing.rb', line 9

def aT_includes element, what_is_element=nil
  e = what_is_element ? what_is_element.to_s.capitalize :
    "Element (#{element.class} instance)"
  m = "#{e} is absent from the array."
  raise TErr, m unless include? element
  return self
end

#each_consecutive_pairObject

Does things for each consecutive pair (expects a binary block).



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/y_support/core_ext/array/misc.rb', line 17

def each_consecutive_pair
  if block_given?
    return self if ( n = self.size - 1 ) <= 0
    n.times.with_index{|i| yield( self[i], self[i+1] ) }
    return self
  else
    return Enumerator.new do |yielder|
      n.times.with_index{|i| yielder << [ self[i], self[i+1] ] } unless
        ( n = self.size - 1 ) <= 0
    end
  end
end

#to_hash(tail_from = 1) ⇒ Object

Converts an array, whose elements are also arrays, to a hash. Head (position 0) of each array is made to point at the rest of the array (tail), normally starting immediately after the head (position 1). The starting position of the tail can be controlled by an optional argument. Tails of 2 and more elements are represented as arrays.



8
9
10
11
12
13
# File 'lib/y_support/core_ext/array/misc.rb', line 8

def to_hash( tail_from = 1 )
  self.reject { | e | e[0].nil? }.reduce({}) { |a, e|
    tail = e[tail_from..-1]
    a.merge( { e[0] => tail.size >= 2 ? tail : tail[0] } )
  }
end

#to_procObject

Allows style &[ function, *arguments ]



32
33
34
# File 'lib/y_support/core_ext/array/misc.rb', line 32

def to_proc
  proc { |receiver| receiver.send *self }
end