class Fakir::Array
Extends the ::Array class to return random elements, where an element is returned only once. rand_reset
resets the rand
method to return elements from the original array, i.e., to possibly repeat elements from before rand_reset
was invoked.
Public Class Methods
new(*args)
click to toggle source
Calls superclass method
# File lib/fakir/array.rb, line 9 def initialize(*args) @used = ::Array.new rand_reset super end
Public Instance Methods
rand()
click to toggle source
# File lib/fakir/array.rb, line 20 def rand if size == 0 raise "error: cannot get random element from an empty array" end idx = Kernel::rand size val = delete_at idx @used << val val end
rand_reset()
click to toggle source
# File lib/fakir/array.rb, line 15 def rand_reset concat @used @used = ::Array.new end