15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/random_phrase.rb', line 15
def phrase(*args)
options = args.
word_patterns = []
unless args.empty?
if args.first.respond_to?(:times)
word_count = args.first
elsif args.first.kind_of?(Regexp)
word_patterns = args.first.source.split(/(?:\s|\\\\s)/)
word_count = word_patterns.count
elsif args.first.respond_to?(:to_s)
word_count = args.first.to_s.split(/\s/).count
end
end
word_count ||= 1
phrase = []
word_count.times do |i|
options.merge!(:pattern => Regexp.compile(word_patterns[i])) unless word_patterns.empty?
phrase << dictionary.words(options).sample
end
phrase.join(" ")
end
|