forked from openlogic/random-word
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'random_word' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') | ||
|
||
describe "RandomWord" do | ||
it "fails" do | ||
fail "hey buddy, you should probably rename this file and start specing for real" | ||
it "defines the module" do | ||
defined?(RandomWord).should be_true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require File.expand_path("spec_helper", File.dirname(__FILE__)) | ||
|
||
describe RandomWord, "enumerator" do | ||
subject {RandomWord.enumerator(["aaa", "bbb", "ccc"])} | ||
|
||
it "can get you the next word in it's list" do | ||
subject.next.should be_one_of(["aaa", "bbb", "ccc"]) | ||
end | ||
|
||
it "raises error when it runs out of words" do | ||
3.times{subject.next} | ||
|
||
lambda{subject.next}.should raise_error(StopIteration) | ||
end | ||
|
||
# This test might pass sometimes even if the code it wrong. It if | ||
# ever fails it is a serious issue and this test should be run | ||
# multiple times before deciding the issue has been fixed. | ||
it "should only return a word one time" do | ||
already_received = [] | ||
3.times do | ||
(new_word = subject.next).should_not be_one_of(already_received) | ||
already_received << new_word | ||
end | ||
end | ||
end | ||
|
||
describe RandomWord do | ||
it "can return a random noun enumerator" do | ||
RandomWord.nouns.should respond_to(:next) | ||
end | ||
|
||
it "can return a random adj enumerator" do | ||
RandomWord.adjs.should respond_to(:next) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
RSpec::Matchers.define :be_one_of do |expected| | ||
match do |actual| | ||
expected.any?{|exp_val| exp_val === actual} | ||
end | ||
|
||
end |