Module: Enumerable

Defined in:
lib/patches/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#cons(k) ⇒ Object

Returns an array of all consecutive k-tuples in the collection.

Parameters:

  • k, the size of the tuples.



7
8
9
10
11
# File 'lib/patches/enumerable.rb', line 7

def cons(k)
  k_tuples = []
  each_cons(k) { |tuple| k_tuples << tuple }
  return k_tuples
end

#exclude?(v) ⇒ Boolean

Checks whether this collection does not contain a given value V.

Parameters:

  • v, the value to check for.

Returns: true if not contained, false if contained.

Returns:

  • (Boolean)


20
21
22
# File 'lib/patches/enumerable.rb', line 20

def exclude?(v)
  not include?(v)
end

#nmax(n) ⇒ Object

Calculates and returns the N highest values from this collection.

Parameters:

  • n, number of values to return.



28
29
30
31
32
33
34
# File 'lib/patches/enumerable.rb', line 28

def nmax(n)
  result = [] # lowest to highest
  each do |v|
    result = result.sorted_insert!(v).last(n) if result.length < n or v > result.first
  end
  return result
end

#nmin(n) ⇒ Object

Calculates and returns the N lowest values from this collection.

Parameters:

  • n, number of values to return.



40
41
42
43
44
45
46
# File 'lib/patches/enumerable.rb', line 40

def nmin(n)
  result = [] # lowest to highest
  each do |v|
    result = result.sorted_insert!(v).last(n) if result.length < n or v < result.first
  end
  return result
end

#sorted_insert!(value) ⇒ Object

Inserts a given value into this collection, assuming that the collection is sorted.

Parameters:

  • value, the value to insert into the collection.

Returns: The resulting collection.



56
57
58
59
60
# File 'lib/patches/enumerable.rb', line 56

def sorted_insert!(value)
  index = calculate_sorted_index(value)
  insert(index, value)
  return self
end