Module: RandomWord

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

Overview

Provides random, non-repeating enumerators of a large list of english words. For example“

RandomWord.adjs.next #=> "strengthened"

Defined Under Namespace

Modules: EachRandomly

Constant Summary collapse

VERSION =
'2.1.1'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.word_listObject

Returns the value of attribute word_list.



73
74
75
# File 'lib/random-word/lib/random_word.rb', line 73

def word_list
  @word_list
end

Class Method Details

.adjs(opts = {}) ⇒ Enumerator

Returns Random adjective enumerator.

Returns:

  • (Enumerator)

    Random adjective enumerator



87
88
89
90
91
# File 'lib/random-word/lib/random_word.rb', line 87

def adjs(opts = {})
  @adjs ||= enumerator(load_word_list("adjs.json"), exclude_list)
  word_list.set_constraints(opts)
  @adjs
end

.enumerator(word_list, list_of_regexs_or_strings_to_exclude = []) ⇒ Object

Create a random, non-repeating enumerator for a list of words (or anything really).



106
107
108
109
110
111
# File 'lib/random-word/lib/random_word.rb', line 106

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_listObject



75
76
77
# File 'lib/random-word/lib/random_word.rb', line 75

def exclude_list
  @exclude_list ||= []
end

.nouns(opts = {}) ⇒ Enumerator

Returns Random noun enumerator.

Returns:

  • (Enumerator)

    Random noun enumerator



80
81
82
83
84
# File 'lib/random-word/lib/random_word.rb', line 80

def nouns(opts = {})
  @nouns ||= enumerator(load_word_list("nouns.json"), exclude_list)
  word_list.set_constraints(opts)
  @nouns
end

.phrasesEnumerator

Returns Random phrase enumerator.

Returns:

  • (Enumerator)

    Random phrase enumerator



94
95
96
97
98
99
100
101
102
# File 'lib/random-word/lib/random_word.rb', line 94

def phrases
  @phrases ||= (Class.new do
    def each()
      while true
        yield "#{RandomWord.adjs.next} #{RandomWord.nouns.next}"
      end
    end
  end.new).to_enum
end