Class: Array

Inherits:
Object show all
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

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.

Parameters:

  • other (#to_a)

    the other Enumerable

Raises:

  • (ArgumentError)

    if other does not respond to the to_a method



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__flattenObject (private)



81
# File 'lib/jinx/helpers/array.rb', line 81

alias :base__flatten :flatten

#equal__baseObject



345
# File 'lib/jinx/import/java.rb', line 345

alias :equal__base :==

#flattenObject

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.

Yields:

  • (item)

    the retention test

Yield Parameters:

  • item

    an item in this array

Returns:



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

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

Returns:

  • (Array)

    an array the tail of this array



27
28
29
# File 'lib/jinx/helpers/array.rb', line 27

def rest
  self[1..-1]
end

#to_assoc_hashHash

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.

Examples:

[[:a, 1], [:b, 2, 3], [:c], []].to_assoc_hash #=> { :a => 1, :b => [2,3], :c => nil }

Returns:

  • (Hash)

    the first => rest hash



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