Sha256: 0b8213aa80522bfaba1c976b8e44badaae8c201078c683d99021eb39889045fe

Contents?: true

Size: 694 Bytes

Versions: 3

Compression:

Stored size: 694 Bytes

Contents

class Array

  # Returns a list of elements that occur +n+ times.
  #
  # If +n+ is a Range then returns elements that occur a number
  # of time within the range.
  #
  # Examples
  #
  #  [0,1,1,1,3,0,1,2,4].occur(3) #=> [1]
  #
  #  [0,1,1,1,3,0,1,2,4].occur(2..4) #=> [0,1]
  #
  # Returns [Array] of reoccurring elements.
  #
  # CREDIT: Robert Dober

  def occur(n=nil) #:yield:
    h = Hash.new(0)

    each do |i|
      h[i] += 1
    end

    case n
    when nil
      h.delete_if{ |_,v| ! yield(v) }.keys
    when Range
      h.delete_if{ |_,v| ! n.include?(v) }.keys
    else
      h.delete_if{|_,v| v != n}.keys
    end
  end

  # Plural alias of #occur.
  alias :occurs :occur

end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/array/occur.rb
facets-3.1.0 lib/core/facets/array/occur.rb
facets-3.0.0 lib/core/facets/array/occur.rb