Class: Array
- 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
-
#>>(collection) ⇒ Object
Zips this array with another collection into a hash.
-
#aT_includes(element, what_is_element = nil) ⇒ Object
(also: #aT_include)
This method takes a block and fails with TypeError, if the receiver array fails to include the specified element.
-
#names(option = nil) ⇒ Object
Maps an array to an array of the element names, obtained by applying
#name
method to them. -
#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.
-
#pop_ordered ⇒ Object
With array construction syntax [:foo, bar: 42] now possible in Ruby, arrays become closer to argument collections, and supporting methods might come handy.
-
#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.
-
#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.
-
#to_hash(tail_from = 1) ⇒ Object
Converts an array, whose elements are also arrays, to a hash.
-
#to_proc ⇒ Object
Allows style &[ function, *arguments ].
-
#zip_to_hash(collection = nil) ⇒ Object
Zips this array with another collection into a hash.
Instance Method Details
#>>(collection) ⇒ Object
Zips this array with another collection into a hash.
33 34 35 |
# File 'lib/y_support/core_ext/array/misc.rb', line 33 def >> collection zip_to_hash collection end |
#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).
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 |
#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.
78 79 80 81 |
# File 'lib/y_support/core_ext/array/misc.rb', line 78 def pop_named key l = last l.delete( key ).tap { pop if l.empty? } if l.is_a? Hash end |
#pop_ordered ⇒ 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.
68 69 70 71 72 |
# File 'lib/y_support/core_ext/array/misc.rb', line 68 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.
58 59 60 61 62 |
# File 'lib/y_support/core_ext/array/misc.rb', line 58 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.
48 49 50 51 |
# File 'lib/y_support/core_ext/array/misc.rb', line 48 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_proc ⇒ Object
Allows style &[ function, *arguments ]
39 40 41 |
# File 'lib/y_support/core_ext/array/misc.rb', line 39 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.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/y_support/core_ext/array/misc.rb', line 19 def zip_to_hash collection=nil if block_given? then fail ArgumentError, "Argument not allowed if block given!" unless collection.nil? Hash[ zip( collection.map { |e| yield e } ) ] else fail ArgumentError "A second collection expected as an argument!" unless collection.respond_to? :each Hash[ zip( collection ) ] end end |