Permalink
Browse files

Separate methods for two- and three-word phrases

  • Loading branch information...
1 parent 727fffe commit d5ab509fa6bf349f2d47007797d879ef70f996fc @kerryb committed Jul 14, 2009
Showing with 9 additions and 3 deletions.
  1. +9 −3 1.rb
View
12 1.rb
@@ -8,22 +8,28 @@ module StringExtensions
def phrases(number)
words = split
all_strings = words
- all_strings += double_triple_words(words)
+ all_strings += double_words(words)
+ all_strings += triple_words(words)
all_strings[0, number].map {|s| "'#{s}'"}.join(', ')
end
private
- def double_triple_words(strings)
+ def double_words(strings)
all_strings = []
num_words = strings.size
# Extracting double-word phrases
(0...num_words - 1).each do |i|
all_strings << "#{strings[i]} #{strings[i + 1]}"
end
+ return all_strings
+ end
+
+ def triple_words(strings)
+ all_strings = []
+ num_words = strings.size
- # Extracting triple-word phrases
(0...num_words - 2).each do |i|
all_strings << "#{strings[i]} #{strings[i + 1]} #{strings[i + 2]}"
end

0 comments on commit d5ab509

Please sign in to comment.