Module: MoreMath::RankingCommon

Included in:
Permutation, Subset
Defined in:
lib/more_math/ranking_common.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#collectionObject (readonly)

Returns the collection the #rank applies to if any was set, otherwise retrurns nil.



15
16
17
# File 'lib/more_math/ranking_common.rb', line 15

def collection
  @collection
end

#lastObject (readonly)

Returns the rank of the last ranked instance.



11
12
13
# File 'lib/more_math/ranking_common.rb', line 11

def last
  @last
end

#rankObject (readonly)

Returns the rank of this instance, a Fixnum in the range of 0 and #last.



8
9
10
# File 'lib/more_math/ranking_common.rb', line 8

def rank
  @rank
end

#sizeObject (readonly)

Returns the size of this instance’s collection, a Fixnum.



4
5
6
# File 'lib/more_math/ranking_common.rb', line 4

def size
  @size
end

Instance Method Details

#eachObject

Iterates over all instances starting with the first (rank == 0) ranked instance and ending with the last (rank == #last) ranked instance while yielding to a freshly created instance for every iteration step.

The mixed in methods from the Enumerable module rely on this method.



66
67
68
69
70
71
72
73
# File 'lib/more_math/ranking_common.rb', line 66

def each # :yields: perm
  0.upto(last) do |r|
    klon = clone
    klon.rank = r
    yield klon
  end
  self
end

#each!Object

Does something similar to #each. It doesn’t create new instances (less overhead) for every iteration step, but yields to a modified self instead. This is useful if one only wants to call a method on the yielded value and work with the result of this call. It’s not a good idea to put the yielded values in a data structure because all of them will reference the same (this!) instance. If you want to do this use #each.



82
83
84
85
86
87
88
89
90
91
# File 'lib/more_math/ranking_common.rb', line 82

def each!
  old_rank = rank
  0.upto(last) do |r|
    self.rank = r
    yield self
  end
  self
ensure
  self.rank = old_rank
end

#nextObject Also known as: succ

Returns the next ranked instance. If this instance is the #last instance it returns the first (rank == 0) instance again.



28
29
30
# File 'lib/more_math/ranking_common.rb', line 28

def next
  clone.next!
end

#next!Object Also known as: succ!

Switches this instance to the next ranked instance. If this was the #last instance it wraps around the first (rank == 0) instance.



19
20
21
22
# File 'lib/more_math/ranking_common.rb', line 19

def next!
  self.rank += 1
  self
end

#predObject

Returns the previously ranked instance. If this was the first instance it returns the last (rank == #last) instance.



43
44
45
# File 'lib/more_math/ranking_common.rb', line 43

def pred
  clone.pred!
end

#pred!Object

Switches this instance to the previously ranked instance. If this was the first instance it returns the last (rank == #last) instance.



36
37
38
39
# File 'lib/more_math/ranking_common.rb', line 36

def pred!
  self.rank -= 1
  self
end

#randomObject

Returns a randomly ranked instance.



55
56
57
# File 'lib/more_math/ranking_common.rb', line 55

def random
  clone.random!
end

#random!Object

Switches this instance to a randomly ranked instance.



48
49
50
51
52
# File 'lib/more_math/ranking_common.rb', line 48

def random!
  new_rank = rand(last + 1).to_i
  self.rank = new_rank
  self
end