Sha256: ed78f858e3835ba0fb7a432d87c51ef2dff684bc99dd1e5542fb500390cf23d2

Contents?: true

Size: 410 Bytes

Versions: 7

Compression:

Stored size: 410 Bytes

Contents

class Array
  #
  # Returns the _only_ element in the array.  Raises an IndexError if
  # the array's size is not 1.
  #
  #   [5].only      # -> 5
  #   [1,2,3].only  # -> IndexError
  #   [].only       # -> IndexError
  #
  #   CREDIT Noah Gibbs
  #   CREDIT Gavin Sinclair

  def only
    unless size == 1
      raise IndexError, "Array#only called on non-single-element array"
    end
    first
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
facets-2.1.2 lib/core/facets/array/only.rb
facets-2.0.3 lib/core/facets/array/only.rb
facets-2.0.4 lib/core/facets/array/only.rb
facets-2.0.5 lib/core/facets/array/only.rb
facets-2.1.0 lib/core/facets/array/only.rb
facets-2.1.1 lib/core/facets/array/only.rb
facets-2.1.3 lib/core/facets/array/only.rb