Sha256: 7a9858fdba0e3d8f44d9c7c05e2679a4b08ce66bb1af540127e193bf17e8e77c

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

Contents

class Array

  # 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
  def before(value)
    return nil unless include? value
    self[(index(value).to_i - 1) % length]
  end

  # Returns the value after the given value. The value before the last
  # is the first. Returns nil if the given value is not in the array.
  #
  # Examples
  #
  #   sequence = ['a', 'b', 'c']
  #   sequence.after('a')           #=> 'b'
  #   sequence.after('b')           #=> 'c'
  #   sequence.after('c')           #=> 'a'
  #   sequence.after('d')           #=> nil
  #
  # CREDIT: Tyler Rick
  def after(value)
    return nil unless include? value
    self[(index(value).to_i + 1) % length]
  end

end

Version data entries

7 entries across 6 versions & 1 rubygems

Version Path
facets-2.9.3 lib/core/facets/array/before.rb
facets-2.9.2 lib/core/facets/array/before.rb
facets-2.9.2 src/core/facets/array/before.rb
facets-2.9.1 lib/core/facets/array/before.rb
facets-2.9.0 lib/core/facets/array/before.rb
facets-2.9.0.pre.2 lib/core/facets/array/before.rb
facets-2.9.0.pre.1 lib/core/facets/array/before.rb