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_self = "array", 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.
-
#aT_uniq(what_is_self = "array") ⇒ Object
Fails with TypeError if the array contains duplicates (using
#uniq). -
#correspondence_matrix(other) ⇒ Object
Returns correspondence matrix to another array.
-
#indices_of(other) ⇒ Object
Returns indices of elements of another array in this array (inverse of #values_at).
-
#names(option = nil) ⇒ Object
Maps an array to an array of the element names, obtained by applying
#namemethod 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_column_vector ⇒ Object
Converts the array to a Matrix#column_vector.
-
#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.
36 37 38 |
# File 'lib/y_support/core_ext/array/misc.rb', line 36 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.
94 95 96 |
# File 'lib/y_support/core_ext/array/misc.rb', line 94 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).
101 102 103 |
# File 'lib/y_support/core_ext/array/misc.rb', line 101 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.
81 82 83 84 |
# File 'lib/y_support/core_ext/array/misc.rb', line 81 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.
71 72 73 74 75 |
# File 'lib/y_support/core_ext/array/misc.rb', line 71 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.
61 62 63 64 65 |
# File 'lib/y_support/core_ext/array/misc.rb', line 61 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.
51 52 53 54 |
# File 'lib/y_support/core_ext/array/misc.rb', line 51 def push_ordered element return push element unless last.is_a? Hash push pop.tap { push element } end |
#to_column_vector ⇒ Object
Converts the array to a Matrix#column_vector.
88 89 90 |
# File 'lib/y_support/core_ext/array/misc.rb', line 88 def to_column_vector Matrix.column_vector self end |
#to_proc ⇒ Object
Allows style &[ function, *arguments ]
42 43 44 |
# File 'lib/y_support/core_ext/array/misc.rb', line 42 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.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/y_support/core_ext/array/misc.rb', line 22 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 |