Module: Nuggets::Array::LimitMixin

Included in:
Array
Defined in:
lib/nuggets/array/limit_mixin.rb

Instance Method Summary collapse

Instance Method Details

#cap(max) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/nuggets/array/limit_mixin.rb', line 45

def cap(max)
  if max.respond_to?(:begin)
    min, max = max.begin, max.end
    map { |item| item.limit(min, max) }
  else
    map { |item| item.max(max) }
  end
end

#limit(min, max, uniq = true) ⇒ Object Also known as: between

call-seq:

array.limit(min, max) => anArray

Returns a new array of all distinct values in array limited to min and max (cf. Numeric#limit). If uniq is true, resulting duplicates will be removed.



37
38
39
40
41
# File 'lib/nuggets/array/limit_mixin.rb', line 37

def limit(min, max, uniq = true)
  limited = cap(min..max)
  limited.uniq! if uniq
  limited
end