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

#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.



56
57
58
59
# File 'lib/y_support/core_ext/array/misc.rb', line 56

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.



46
47
48
49
50
# File 'lib/y_support/core_ext/array/misc.rb', line 46

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.



36
37
38
39
40
# File 'lib/y_support/core_ext/array/misc.rb', line 36

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.



26
27
28
29
# File 'lib/y_support/core_ext/array/misc.rb', line 26

def push_ordered element
  return push element unless last.is_a? Hash
  push pop.tap { push element }
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 ]



17
18
19
# File 'lib/y_support/core_ext/array/misc.rb', line 17

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