module MoreMath::RankingCommon
def each # :yields: perm
The mixed in methods from the Enumerable module rely on this method.
step.
yielding to a freshly created instance for every iteration
last (
rank == #last
) ranked instance whilefirst (
rank == 0
) ranked instance and ending with theIterates over all instances starting with the
def each # :yields: perm 0.upto(last) do |r| klon = clone klon.rank = r yield klon end self end
def each!
of them will reference the same (this!) instance. If you want to do this
not a good idea to put the yielded values in a data structure because all
method on the yielded value and work with the result of this call. It's
modified self instead. This is useful if one only wants to call a
instances (less overhead) for every iteration step, but yields to a
Does something similar to #each. It doesn't create new
def each! old_rank = rank 0.upto(last) do |r| self.rank = r yield self end self ensure self.rank = old_rank end
def next
rank == 0
) instance again.Returns the next ranked instance. If this instance is the #last instance
def next clone.next! end
def next!
rank == 0
) instance.Switches this instance to the next ranked instance. If this was the #last
def next! self.rank += 1 self end
def pred
rank == #last
) instance.Returns the previously ranked instance. If this was the first instance it
def pred clone.pred! end
def pred!
rank == #last
) instance.Switches this instance to the previously ranked instance. If this was the
def pred! self.rank -= 1 self end
def random
def random clone.random! end
def random!
def random! new_rank = rand(last + 1).to_i self.rank = new_rank self end