Class: Seq::Random

Inherits:
Seq
  • Object
show all
Defined in:
lib/seq/random.rb

Overview

A Random seq chooses items from the list randomly.

Examples:


s = Seq::Random.new([1,2,3,4,5])
s.take(6) #=> [1, 4, 5, 3, 1, 2]

Constant Summary

Constants inherited from Seq

VERSION

Instance Method Summary collapse

Methods inherited from Seq

#each, #ended?, #entries, #inc, #infinite?, #method_missing, #reset, #to_a

Constructor Details

#initialize(list = [], items = Float::INFINITY, default = nil) ⇒ Random

Creates a new Random seq instance.

Parameters:

  • list (Array) (defaults to: [])

    List of values to cycle over

  • items (Integer) (defaults to: Float::INFINITY)

    Number of values to return

  • default (Object) (defaults to: nil)

    Value to return when finished cycling



19
20
21
# File 'lib/seq/random.rb', line 19

def initialize(list=[], items=Float::INFINITY, default=nil)
  super(list, items, 0, default)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Seq

Instance Method Details

#nextObject

Returns Until ended it returns a randomly selected item from the list, when ended it returns the default value.

Returns:

  • Until ended it returns a randomly selected item from the list, when ended it returns the default value.



25
26
27
28
29
30
31
# File 'lib/seq/random.rb', line 25

def next
  if ended?
    @default
  else
    @list[rand(@list.size).to_i].tap { inc }
  end
end