forked from openlogic/random-word
/
random_word.rb
55 lines (44 loc) · 1 KB
/
random_word.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'set'
module RandomWord
module EachRandomly
def each_randomly(&blk)
used = Set.new
while true
idx = next_unused_idx(used)
used << idx
yield at(idx)
end
rescue OutOfWords
# we are done.
end
private
def next_unused_idx(used)
idx = rand(length)
try = 1
while used.include?(idx)
raise OutOfWords if try > 1000
idx = rand(length)
try += 1
end
idx
end
class OutOfWords < Exception; end
end
class << self
def enumerator(word_list)
word_list.extend EachRandomly
Enumerator.new(word_list, :each_randomly)
end
def nouns
@nouns ||= enumerator(load_word_list("nouns.dat"))
end
def adjs
@adjs ||= enumerator(load_word_list("adjs.dat"))
end
protected
def load_word_list(name)
data_root = Pathname(File.dirname(__FILE__)) + "../data"
File.open(data_root + name){|f| Marshal.load(f)}
end
end
end