Sha256: e2922b45bee1974fdc112890dafda7c4bd259ca6bf168eae2116d893ea36d208

Contents?: true

Size: 395 Bytes

Versions: 10

Compression:

Stored size: 395 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: Gavin Sinclair, Noah Gibbs

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

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/array/only.rb
facets-2.8.3 lib/core/facets/array/only.rb
facets-2.8.2 lib/core/facets/array/only.rb
facets-2.8.1 lib/core/facets/array/only.rb
facets-2.8.0 lib/core/facets/array/only.rb
facets-2.7.0 lib/core/facets/array/only.rb
facets-2.6.0 lib/core/facets/array/only.rb
facets-2.5.1 lib/core/facets/array/only.rb
facets-2.5.0 lib/core/facets/array/only.rb
facets-2.5.2 lib/core/facets/array/only.rb