instance method sole

Ruby on Rails 7.2.3

Since v7.2.3

Available in: v7.2.3 v8.0.4 v8.1.2

Signature

sole()

Returns the sole item in the range. If there are no items, or more than one item, raises Enumerable::SoleItemExpectedError.

(1..1).sole   # => 1
(2..1).sole   # => Enumerable::SoleItemExpectedError: no item found
(..1).sole    # => Enumerable::SoleItemExpectedError: infinite range cannot represent a sole item
Source
# File activesupport/lib/active_support/core_ext/range/sole.rb, line 10
  def sole
    if self.begin.nil? || self.end.nil?
      raise ActiveSupport::EnumerableCoreExt::SoleItemExpectedError, "infinite range '#{inspect}' cannot represent a sole item"
    end

    super
  end

Defined in activesupport/lib/active_support/core_ext/range/sole.rb line 10 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Range

Type at least 2 characters to search.

↑↓ navigate · open · esc close