Class: Array
- Includes:
- Jinx::Collection
- Defined in:
- lib/jinx/import/java.rb,
lib/jinx/helpers/array.rb,
lib/jinx/helpers/merge.rb,
lib/jinx/helpers/collections.rb
Defined Under Namespace
Classes: EMPTY_ARRAY||=Array.new
Instance Method Summary collapse
-
#==(other) ⇒ Object
Overrides the standard == to compare a Java List with a Ruby Array.
-
#add_all(other) ⇒ Object
(also: #merge!)
Concatenates the other Enumerable to this array.
- #base__flatten ⇒ Object private
- #equal__base ⇒ Object
-
#flatten ⇒ Object
Recursively flattens this array, including any collection item that implements the
to_a
method. -
#keep_if {|item| ... } ⇒ Array
Deletes items from this array which do not satisfy the given block.
-
#merge(other) ⇒ Object
Adds the elements in the other Enumerable which are not already included in this Array.
-
#rest ⇒ Array
(also: #tail)
Returns an array containing all but the first item in this Array.
-
#to_assoc_hash ⇒ Hash
Returns a new Hash generated from this array of arrays by associating the first element of each member to the remaining elements.
-
#to_series(conjunction = nil) ⇒ Object
Prints the content of this array as a series, e.g.: [1, 2, 3].to_series #=> “1, 2 and 3” [1, 2, 3].to_series(‘or’) #=> “1, 2 or 3”.
Methods included from Jinx::Collection
#compact, #compact_map, #detect_value, #detect_with_value, #difference, #empty?, #filter, #first, #hashify, #intersect, #join, #last, #partial_sort, #partial_sort!, #partial_sort_by, #size, #to_compact_hash, #to_compact_hash_with_index, #transform, #union
Instance Method Details
#==(other) ⇒ Object
Overrides the standard == to compare a Java List with a Ruby Array.
347 348 349 |
# File 'lib/jinx/import/java.rb', line 347 def ==(other) Java::JavaUtil::List === other ? other == self : equal__base(other) end |
#add_all(other) ⇒ Object Also known as: merge!
Concatenates the other Enumerable to this array.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/jinx/helpers/array.rb', line 96 def add_all(other) return concat(other) if Array === other begin add_all(other.to_a) rescue NoMethodError raise e rescue raise ArgumentError.new("Can't convert #{other.class.name} to array") end end |
#base__flatten ⇒ Object (private)
81 |
# File 'lib/jinx/helpers/array.rb', line 81 alias :base__flatten :flatten |
#equal__base ⇒ Object
345 |
# File 'lib/jinx/import/java.rb', line 345 alias :equal__base :== |
#flatten ⇒ Object
Recursively flattens this array, including any collection item that implements the to_a
method.
84 85 86 87 88 89 90 |
# File 'lib/jinx/helpers/array.rb', line 84 def flatten # if any item is a Set or Java Collection, then convert those into arrays before recursively flattening the list if any? { |item| Set === item or Java::JavaUtil::Collection === item } then return map { |item| (Set === item or Java::JavaUtil::Collection === item) ? item.to_a : item }.flatten end base__flatten end |
#keep_if {|item| ... } ⇒ Array
Deletes items from this array which do not satisfy the given block.
38 39 40 |
# File 'lib/jinx/helpers/array.rb', line 38 def keep_if delete_if { |item| not yield(item) } end |
#merge(other) ⇒ Object
Adds the elements in the other Enumerable which are not already included in this Array. Returns this modified Array.
53 54 55 56 57 58 59 |
# File 'lib/jinx/helpers/merge.rb', line 53 def merge(other) # incompatible merge argument is allowed but ignored self unless Enumerable === other # concatenate the members of other not in self unique = other.to_a - self concat(unique) end |
#rest ⇒ Array Also known as: tail
Returns an array containing all but the first item in this Array. This method is syntactic sugar for self[1..-1]
or last(length-1).
27 28 29 |
# File 'lib/jinx/helpers/array.rb', line 27 def rest self[1..-1] end |
#to_assoc_hash ⇒ Hash
Returns a new Hash generated from this array of arrays by associating the first element of each member to the remaining elements. If there are only two elements in the member, then the first element is associated with the second element. If there is less than two elements in the member, the first element is associated with nil. An empty array is ignored.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/jinx/helpers/array.rb', line 64 def to_assoc_hash hash = {} each do |item| raise ArgumentError.new("Array member must be an array: #{item.pp_s(:single_line)}") unless Array === item key = item.first if item.size < 2 then value = nil elsif item.size == 2 then value = item[1] else value = item[1..-1] end hash[key] = value unless key.nil? end hash end |
#to_series(conjunction = nil) ⇒ Object
Prints the content of this array as a series, e.g.:
[1, 2, 3].to_series #=> "1, 2 and 3"
[1, 2, 3].to_series('or') #=> "1, 2 or 3"
If a block is given to this method, then the block is applied before the series is formed, e.g.:
[1, 2, 3].to_series { |n| n + 1 } #=> "2, 3 and 4"
48 49 50 51 52 53 54 |
# File 'lib/jinx/helpers/array.rb', line 48 def to_series(conjunction=nil) conjunction ||= 'and' return map { |item| yield item }.to_series(conjunction) if block_given? padded_conjunction = " #{conjunction} " # join all but the last item as a comma-separated list and append the conjunction and last item length < 2 ? to_s : self[0...-1].join(', ') + padded_conjunction + last.to_s end |