Sha256: a698718aa88362d212c89a1b9c7a7a971050bfef32ef0389d7423c95f493d919

Contents?: true

Size: 408 Bytes

Versions: 7

Compression:

Stored size: 408 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
  #  CREDIT: Noah Gibbs

  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 & 2 rubygems

Version Path
facets-2.4.0 lib/facets/array/only.rb
facets-2.4.1 lib/facets/array/only.rb
facets-2.4.2 lib/core/facets/array/only.rb
facets-2.4.3 lib/core/facets/array/only.rb
facets-2.4.4 lib/core/facets/array/only.rb
facets-2.4.5 lib/core/facets/array/only.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/array/only.rb