Module: MoreMath::RankingCommon
- Included in:
- Permutation, Subset
- Defined in:
- lib/more_math/ranking_common.rb
Overview
Common functionality for ranking systems. This module provides basic ranking operations that are shared across different ranking implementations in the MoreMath library, including permutations and subsets.
Instance Attribute Summary collapse
-
#collection ⇒ Object?
readonly
Returns the collection the #rank applies to if any was set, otherwise returns nil.
-
#last ⇒ Integer
readonly
Returns the rank of the last ranked instance.
-
#rank ⇒ Integer
readonly
Returns the rank of this instance, a Fixnum in the range of 0 and #last.
-
#size ⇒ Integer
readonly
Returns the size of this instance’s collection, a Fixnum.
Instance Method Summary collapse
-
#each {|instance| ... } ⇒ self
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. -
#each! {|instance| ... } ⇒ self
Does something similar to #each.
-
#next ⇒ self
(also: #succ)
Returns the next ranked instance.
-
#next! ⇒ self
(also: #succ!)
Switches this instance to the next ranked instance.
-
#pred ⇒ self
Returns the previously ranked instance.
-
#pred! ⇒ self
Switches this instance to the previously ranked instance.
-
#random ⇒ self
Returns a randomly ranked instance.
-
#random! ⇒ self
Switches this instance to a randomly ranked instance.
Instance Attribute Details
#collection ⇒ Object? (readonly)
Returns the collection the #rank applies to if any was set, otherwise returns nil.
26 27 28 |
# File 'lib/more_math/ranking_common.rb', line 26 def collection @collection end |
#last ⇒ Integer (readonly)
Returns the rank of the last ranked instance.
20 21 22 |
# File 'lib/more_math/ranking_common.rb', line 20 def last @last end |
#rank ⇒ Integer (readonly)
Returns the rank of this instance, a Fixnum in the range of 0 and #last.
15 16 17 |
# File 'lib/more_math/ranking_common.rb', line 15 def rank @rank end |
#size ⇒ Integer (readonly)
Returns the size of this instance’s collection, a Fixnum.
9 10 11 |
# File 'lib/more_math/ranking_common.rb', line 9 def size @size end |
Instance Method Details
#each {|instance| ... } ⇒ self
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.
92 93 94 95 96 97 98 99 |
# File 'lib/more_math/ranking_common.rb', line 92 def each 0.upto(last) do |r| klon = clone klon.rank = r yield klon end self end |
#each! {|instance| ... } ⇒ self
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.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/more_math/ranking_common.rb', line 110 def each! old_rank = rank 0.upto(last) do |r| self.rank = r yield self end self ensure self.rank = old_rank end |
#next ⇒ self Also known as: succ
Returns the next ranked instance. If this instance is the #last instance it returns the first (rank == 0) instance again.
43 44 45 |
# File 'lib/more_math/ranking_common.rb', line 43 def next clone.next! end |
#next! ⇒ self Also known as: succ!
Switches this instance to the next ranked instance. If this was the #last instance it wraps around to the first (rank == 0) instance.
32 33 34 35 |
# File 'lib/more_math/ranking_common.rb', line 32 def next! self.rank += 1 self end |
#pred ⇒ self
Returns the previously ranked instance. If this was the first instance it returns the last (rank == #last) instance.
62 63 64 |
# File 'lib/more_math/ranking_common.rb', line 62 def pred clone.pred! end |
#pred! ⇒ self
Switches this instance to the previously ranked instance. If this was the first instance it returns the last (rank == #last) instance.
53 54 55 56 |
# File 'lib/more_math/ranking_common.rb', line 53 def pred! self.rank -= 1 self end |
#random ⇒ self
Returns a randomly ranked instance.
78 79 80 |
# File 'lib/more_math/ranking_common.rb', line 78 def random clone.random! end |
#random! ⇒ self
Switches this instance to a randomly ranked instance.
69 70 71 72 73 |
# File 'lib/more_math/ranking_common.rb', line 69 def random! new_rank = rand(last + 1).to_i self.rank = new_rank self end |