Module: RandomWord::EachRandomly

Defined in:
lib/random-word/lib/random_word.rb

Defined Under Namespace

Classes: OutOfWords

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#max_lengthObject

Returns the value of attribute max_length.



14
15
16
# File 'lib/random-word/lib/random_word.rb', line 14

def max_length
  @max_length
end

#min_lengthObject

Returns the value of attribute min_length.



13
14
15
# File 'lib/random-word/lib/random_word.rb', line 13

def min_length
  @min_length
end

#random_word_exclude_listObject

Returns the value of attribute random_word_exclude_list.



12
13
14
# File 'lib/random-word/lib/random_word.rb', line 12

def random_word_exclude_list
  @random_word_exclude_list
end

Class Method Details

.extended(mod) ⇒ Object



41
42
43
# File 'lib/random-word/lib/random_word.rb', line 41

def self.extended(mod)
  mod.set_constraints
end

Instance Method Details

#each_randomly(&blk) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/random-word/lib/random_word.rb', line 16

def each_randomly(&blk)
  used = Set.new
  skipped = Set.new
  loop do
    idx = next_unused_idx(used)
    used << idx
    word = at(idx)
    if excluded?(word)
      skipped << idx
      next
    end
    yield word
    used.subtract(skipped)
    skipped.clear
  end

rescue OutOfWords
  # we are done.
end

#set_constraints(opts = {}) ⇒ Object



36
37
38
39
# File 'lib/random-word/lib/random_word.rb', line 36

def set_constraints(opts = {})
  @min_length = opts[:not_shorter_than] || 1
  @max_length = opts[:not_longer_than] || Float::INFINITY
end