File: exactly.rb

package info (click to toggle)
ruby-powerpack 0.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 492 kB
  • sloc: ruby: 819; makefile: 3
file content (43 lines) | stat: -rw-r--r-- 1,183 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
unless Enumerable.method_defined? :exactly?
  module Enumerable
    # Checks if exactly n elements meet a certain predicate.
    #
    # @param n [Fixnum] the number of matches required
    # @return [Boolean] true if we get exactly n matches, false otherwise
    #
    # @example
    #   [1, 2, 3, 4].exactly?(1) { |n| n > 3 } #=> true
    #   [1, 2, 3, 4].exactly?(2, &:even?) #=> true
    #   [1, 1, 3, 3].exactly?(2, &:even?) #=> false
    #
    # Without a block uses the identify of the elements as default predicate.
    # This means that nil and false elements will be ignored.
    #
    # @example
    #   [1, false, nil].exactly?(3) #=> false
    #   [1, false, nil].exactly?(1) #=> true
    #   [false, nil].exactly?(0) #=> true
    #   [1, 2, 3].exactly?(3) #=>true
    def exactly?(n)
      found_count = 0

      if block_given?
        each do |*o|
          if yield(*o)
            found_count += 1
            return false if found_count > n
          end
        end
      else
        each do |o|
          if o
            found_count += 1
            return false if found_count > n
          end
        end
      end

      n == found_count
    end
  end
end