Method: Array#before

Defined in:
lib/core/facets/array/before.rb

#before(value) ⇒ Object

Returns the value previous to the given value. The value previous to the first is the last. Returns nil if the given value is not in the array.

Examples

sequence = ['a', 'b', 'c']
sequence.before('a')           #=> 'c'
sequence.before('b')           #=> 'a'
sequence.before('c')           #=> 'b'
sequence.before('d')           #=> nil

CREDIT: Tyler Rick



16
17
18
19
# File 'lib/core/facets/array/before.rb', line 16

def before(value)
  return nil unless include? value
  self[(index(value).to_i - 1) % length]
end