module ArrayUtility

Public Instance Methods

before(single) click to toggle source

Gets the previous value in the Array. Unlike next, this method will never return Nil. Instead it will simply loop back to the end of the Array. @param single [Any] The starting value. @return [Any] The value directly before single.

# File lib/array_utility.rb, line 15
def before(single)
  self[index(single) - 1]
end
next(single) click to toggle source

Gets the next value in the Array. @param single [Any] The starting value. @return [Any] The value directly after single. @return [Nil] If there is no value after single.

# File lib/array_utility.rb, line 7
def next(single)
  self[index(single) + 1]
end
prepend_capped(value, cap) click to toggle source

Puts a value at the start of an array, and deletes all values greater than

the cap.

@param value [Any] The value to prepend. @param cap [Int] The maximum number of values in the array.

# File lib/array_utility.rb, line 23
def prepend_capped(value, cap)
  unshift(value)
  if size > cap
    drop(cap).each do |val|
      delete(val)
    end
  end
  self
end