This is kind of out of general topic. I'm studying Ruby, and hers's
what I wrote for anagram search. I am not sure this is the nicest
solution, but I'm stuck. Any help would be appreciated.
def anagramKey(s)
hash = Hash.new {0}
s.upcase.split(//).grep(/\w/).each { |c| hash[c]+=1 }
hash.sort.join
end
def scan(iter)
hash = Hash.new {|hash, key| hash[key] = Set.new }
iter.each do
|line| line.split(/\s+/).each do
|word|
word.gsub!(/\W/, '')
hash[anagramKey(word)] << word.upcase.to_sym unless word.empty?
end
end
result = []
hash.each{|key, value| result << value.to_a.join(",") if value.length > 1}
result
end
File.open("c:\\tmp\\COPYING") do
| file | puts scan(file)
end