Sha256: e7df002e8c7fa7abb93651a3bcce00dc8a837e109b4702b70b14597c5d61f44a
Contents?: true
Size: 914 Bytes
Versions: 1
Compression:
Stored size: 914 Bytes
Contents
module Enumerable # Like {https://ruby-doc.org/core/Enumerable.html#method-i-first # +Enumerable#first+}, but raises an exception if the Enumerable does # not contain the requested number of elements. # # @example # [7, 8, 9].first # == 7 # [7, 8, 9].first(1) # == [7] # [7, 8, 9].first(2) # == [7, 8] # [7, 8, 9].first(4) # raises exception # [].first # raises exception # # @param n [Integer] # requested number of elements # @return [Object, Array] # @raise [MiniSanity::Error] # if the Enumerable does not contain the requested number of elements def first!(n = nil) result = n.nil? ? self.first : self.first(n) if (result.nil? && !self.any?{ true }) || (!n.nil? && result.length < n) raise MiniSanity::Error.new(nil, "Enumerable having at least #{n || 1} elements", self.inspect) end result end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mini_sanity-1.1.0 | lib/mini_sanity/util/enumerable.rb |