module RandomWord
Provides random, non-repeating enumerators of a large list of english words. For example“
RandomWord.adjs.next #=> "strengthened"
Constants
- VERSION
Attributes
word_list[RW]
Public Class Methods
adjs(opts = {})
click to toggle source
@return [Enumerator] Random adjective enumerator
# File lib/random_word.rb, line 82 def adjs(opts = {}) @adjs ||= enumerator(load_word_list("adjs.json"), exclude_list) word_list.set_constraints(opts) @adjs end
each() { |"#{adjs.next} #{nouns.next}"| ... }
click to toggle source
# File lib/random_word.rb, line 91 def each() while true yield "#{RandomWord.adjs.next} #{RandomWord.nouns.next}" end end
enumerator(word_list, list_of_regexs_or_strings_to_exclude = [])
click to toggle source
Create a random, non-repeating enumerator for a list of words (or anything really).
# File lib/random_word.rb, line 101 def enumerator(word_list, list_of_regexs_or_strings_to_exclude = []) @word_list = word_list word_list.extend EachRandomly word_list.random_word_exclude_list = list_of_regexs_or_strings_to_exclude word_list.enum_for(:each_randomly) end
exclude_list()
click to toggle source
# File lib/random_word.rb, line 70 def exclude_list @exclude_list ||= [] end
nouns(opts = {})
click to toggle source
@return [Enumerator] Random noun enumerator
# File lib/random_word.rb, line 75 def nouns(opts = {}) @nouns ||= enumerator(load_word_list("nouns.json"), exclude_list) word_list.set_constraints(opts) @nouns end
phrases()
click to toggle source
@return [Enumerator] Random phrase enumerator
# File lib/random_word.rb, line 89 def phrases @phrases ||= (Class.new do def each() while true yield "#{RandomWord.adjs.next} #{RandomWord.nouns.next}" end end end.new).to_enum end
Protected Class Methods
load_word_list(name)
click to toggle source
# File lib/random_word.rb, line 110 def load_word_list(name) filename = File.join Pathname(File.dirname(__FILE__)), "../data", name JSON.parse(File.read(filename)) end