Class: Array

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

Instance Method Summary collapse

Instance Method Details

#>>(collection) ⇒ Object

Zips this array with another collection into a hash.



34
35
36
# File 'lib/y_support/core_ext/array/misc.rb', line 34

def >> collection
  zip_to_hash collection
end

#aT_includes(element, what_is_self = "array", 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).



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

def aT_includes element, what_is_self="array", what_is_element=nil
  m = "%s is absent from #{what_is_self}!" %
    if what_is_element then what_is_element.to_s.capitalize else
      "Element (#{element.class} instance)"
    end
  tap { include? element or fail TypeError, m }
end

#aT_uniq(what_is_self = "array") ⇒ Object

Fails with TypeError if the array contains duplicates (using #uniq).



20
21
22
23
# File 'lib/y_support/typing/array/typing.rb', line 20

def aT_uniq what_is_self="array"
  m = "#{what_is_self.to_s.capitalize} non-uniq!"
  tap { self == uniq or fail TypeError, m }
end

#correspondence_matrix(other) ⇒ Object

Returns correspondence matrix to another array.



92
93
94
# File 'lib/y_support/core_ext/array/misc.rb', line 92

def correspondence_matrix other
  Matrix.correspondence_matrix self, other
end

#indices_of(other) ⇒ Object

Returns indices of elements of another array in this array (inverse of #values_at).



99
100
101
# File 'lib/y_support/core_ext/array/misc.rb', line 99

def indices_of other
  other.map { |e| index e }
end

#names(option = nil) ⇒ Object

Maps an array to an array of the element names, obtained by applying #name method to them. Takes one optional argument, which regulates its behavior regarding unnamed objects. If set to nil (default) unnamed objects will be mapped to nil (default behavior of the #name method). If set to true, unnamed objects will be mapped to themselves. If set to false, unnamed object will not be mapped at all – the returned array will contain only the names of the named objects.



12
13
14
15
16
17
# File 'lib/y_support/name_magic/array.rb', line 12

def names option=nil
  return map &:name if option.nil?                 # unnamed --> nil
  return map { |e| e.name || e } if option == true # unnamed --> instance
  return map( &:name ).compact if option == false  # unnamed squeezed out
  fail ArgumentError, "Unknown option: #{option}"
end

#pop_named(key) ⇒ Object

With array construction syntax [:foo, bar: 42] now possible in Ruby, arrays become closer to argument collections, and supporting methods might come handy. This method pops an element from the “ordered arguments” array part.



79
80
81
82
# File 'lib/y_support/core_ext/array/misc.rb', line 79

def pop_named key
  l = last
  l.delete( key ).tap { pop if l.empty? } if l.is_a? Hash
end

#pop_orderedObject

With array construction syntax [:foo, bar: 42] now possible in Ruby, arrays become closer to argument collections, and supporting methods might come handy. This method pops an element from the “ordered arguments” array part.



69
70
71
72
73
# File 'lib/y_support/core_ext/array/misc.rb', line 69

def pop_ordered
  l = pop
  return l unless l.is_a? Hash
  pop.tap { push l }
end

#push_named(**oo) ⇒ Object

With array construction syntax [:foo, bar: 42] now possible in Ruby, arrays become closer to argument collections, and supporting methods might come handy. This method pushes an element on top of the “named arguments” part of the array.



59
60
61
62
63
# File 'lib/y_support/core_ext/array/misc.rb', line 59

def push_named **oo
  l = last
  return push oo unless l.is_a? Hash
  tap { l.update oo }
end

#push_ordered(element) ⇒ Object

With array construction syntax [:foo, bar: 42] now possible in Ruby, arrays become closer to argument collections, and supporting methods might come handy. This method pushes an element on top of the “ordered arguments” part of the array.



49
50
51
52
# File 'lib/y_support/core_ext/array/misc.rb', line 49

def push_ordered element
  return push element unless last.is_a? Hash
  push pop.tap { push element }
end

#to_column_vectorObject

Converts the array to a Matrix#column_vector.



86
87
88
# File 'lib/y_support/core_ext/array/misc.rb', line 86

def to_column_vector
  Matrix.column_vector self
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.



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

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 ]



40
41
42
# File 'lib/y_support/core_ext/array/misc.rb', line 40

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

#zip_to_hash(collection = nil) ⇒ Object

Zips this array with another collection into a hash. If a block is given, it is applied to each element of the array to get the hash values.



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

def zip_to_hash collection=nil
  if block_given? then
    fail ArgumentError, "Argument not allowed if block given!" unless
      collection.nil?
    Hash[ zip( map { |e| yield e } ) ]
  else
    fail ArgumentError "A second collection expected as an argument!" unless
      collection.respond_to? :each
    Hash[ zip( collection ) ]
  end
end