Module: MoreMath::RankingCommon
- Included in:
- Permutation, Subset
- Defined in:
- lib/more_math/ranking_common.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the collection the #rank applies to if any was set, otherwise retrurns nil.
-
#last ⇒ Object
readonly
Returns the rank of the last ranked instance.
-
#rank ⇒ Object
readonly
Returns the rank of this instance, a Fixnum in the range of 0 and #last.
-
#size ⇒ Object
readonly
Returns the size of this instance’s collection, a Fixnum.
Instance Method Summary collapse
-
#each ⇒ Object
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! ⇒ Object
Does something similar to #each.
-
#next ⇒ Object
(also: #succ)
Returns the next ranked instance.
-
#next! ⇒ Object
(also: #succ!)
Switches this instance to the next ranked instance.
-
#pred ⇒ Object
Returns the previously ranked instance.
-
#pred! ⇒ Object
Switches this instance to the previously ranked instance.
-
#random ⇒ Object
Returns a randomly ranked instance.
-
#random! ⇒ Object
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 retrurns nil.
15 16 17 |
# File 'lib/more_math/ranking_common.rb', line 15 def collection @collection end |
#last ⇒ Object (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 |
#rank ⇒ Object (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 |
#size ⇒ Object (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
#each ⇒ Object
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 |
#next ⇒ Object 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 |
#pred ⇒ Object
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 |
#random ⇒ Object
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 |