1 #!/usr/bin/ruby -w
2
3 class String
4 def isPalindrome(min_length = 5)
5 # Match words that:
6 # are not words that repeat one character
7 self !~ /^(\w)\1*$/i &&
8 # are long enough to be interesting
9 length >= min_length &&
10 # have at least one vowel
11 self =~ /[aeiouyw]/i &&
12 # and ... ahem ... are a palindrome
13 self.downcase == reverse.downcase
14 end
15 end
16
17 # find palindromes in a dictionary file
18 def findPalindromes(source,min_len)
19 words = 0
20 list = Array.new
21 File.foreach(source) { |word|
22 word.chomp!
23 if word.isPalindrome(min_len)
24 list << word
25 end
26 words += 1
27 }
28 return words, list
29 end
30 min_length = 3
31 words, list = findPalindromes("/usr/share/dict/words",min_length);
32 puts list.sort.join(", ")
33 puts "words: #{words}, palindromes: #{list.length}"