Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
pezra committed Jun 22, 2011
1 parent 4c16841 commit 6347f76
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 2 deletions.
Binary file added data/adjs.dat
Binary file not shown.
Binary file added data/nouns.dat
Binary file not shown.
1 change: 1 addition & 0 deletions lib/random-word.rb
@@ -0,0 +1 @@
require 'random_word'
55 changes: 55 additions & 0 deletions lib/random_word.rb
@@ -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

4 changes: 2 additions & 2 deletions spec/random-word_spec.rb
@@ -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
37 changes: 37 additions & 0 deletions spec/random_word_spec.rb
@@ -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
6 changes: 6 additions & 0 deletions spec/support/matchers.rb
@@ -0,0 +1,6 @@
RSpec::Matchers.define :be_one_of do |expected|
match do |actual|
expected.any?{|exp_val| exp_val === actual}
end

end

0 comments on commit 6347f76

Please sign in to comment.