Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/rtkit/ruby_extensions.rb

Overview

Array extensions used by RTKIT.

Instance Method Summary collapse

Instance Method Details

#assimilate!(other) ⇒ Object

Rearranges the array (self) so that its elements appear in exactly the same sequence as another array (the argument). If the two arrays do not contain the same set of elements, an error is raised.

Examples

a = [5, 2, 10, 1]
b = [10, 2, 1, 5]
b.assimilate!(a)
b
=> [5, 2, 10, 1]

NB! Not in use atm!



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rtkit/ruby_extensions.rb', line 19

def assimilate!(other)
  if self.length != other.length
    raise ArgumentError, "Arrays 'self' and 'other' are of unequal length. Unable to compare."
  else
    # Validate equality:
    self.each_index do |i|
      index = other.index(self[i])
      if index
        self[i] = other[index]
      else
        raise "An element (index #{i}) in self was not found in the other array. Unable to assimilate."
      end
    end
  end
  return self
end

#compare_with(other) ⇒ Object

Compares an array (self) with a target array to determine an array of indices which can be used to extract the elements of self in order to create an array which shares the exact order of elements as the target array. Naturally, for this comparison to make sense, the target array and self must contain the same set of elements. Raises an error if self and other are not of equal length.

Restrictions

  • Behaviour may be incorrect if the array contains multiple identical objects.

Examples

a = ['hi', 2, dcm]
b = [2, dcm, 'hi']
order = b.compare_with(a)
=> [2, 0, 1]

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rtkit/ruby_extensions.rb', line 54

def compare_with(other)
  raise ArgumentError, "Arrays 'self' and 'other' are of unequal length. Unable to compare." if self.length != other.length
  if self.length > 0
    order = Array.new
    other.each do |item|
      index = self.index(item)
      if index
        order << index
      else
        raise "An element (#{item}) from the other array was not found in self. Unable to complete comparison."
      end
    end
  end
  return order
end

#most_common_valueObject

Returns the most common value in the array.



72
73
74
75
76
# File 'lib/rtkit/ruby_extensions.rb', line 72

def most_common_value
  self.group_by do |e|
    e
  end.values.max_by(&:size).first
end

#sort_by_order(order = []) ⇒ Object

Returns an array where the elements of the original array are extracted according to the indices given in the argument array.

Examples

a = [5, 2, 10, 1]
i = a.sort_order
a.sort_by_order(i)
=> [1, 2, 5, 10]


89
90
91
92
93
94
95
# File 'lib/rtkit/ruby_extensions.rb', line 89

def sort_by_order(order=[])
  if self.length != order.length
    return nil
  else
    return self.values_at(*order)
  end
end

#sort_by_order!(order = []) ⇒ Object

Rearranges an array (self) so that it’s original elements in the order specified by the indices given in the argument array.

Examples

a = [5, 2, 10, 1]
a.sort_by_order!([3, 1, 0, 2])
a
=> [1, 2, 5, 10]

Raises:

  • (ArgumentError)


107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rtkit/ruby_extensions.rb', line 107

def sort_by_order!(order=[])
  raise ArgumentError, "Invalid argument 'order'. Expected length equal to self.length (#{self.length}), got #{order.length}." if self.length != order.length
  # It only makes sense to sort if length is 2 or more:
  if self.length > 1
    copy = self.dup
    self.each_index do |i|
      self[i] = copy[order[i]]
    end
  end
  return self
end

#sort_orderObject

Returns an ordered array of indices, where each element contains the index in the original array which needs to be extracted to produce a sorted array. This method is useful if you wish to sort multiple arrays depending on the sequence of elements in a specific array.

Examples

a = [5, 2, 10, 1]
a.sort_order
=> [3, 1, 0, 2]


129
130
131
132
133
134
135
136
137
# File 'lib/rtkit/ruby_extensions.rb', line 129

def sort_order
  d=[]
  self.each_with_index{|x,i| d[i]=[x,i]}
  if block_given?
    return d.sort {|x,y| yield x[0],y[0]}.collect{|x| x[1]}
  else
    return d.sort.collect{|x| x[1]}
  end
end