December 7th, 2006

nova

first Ruby program

Here is my first Ruby program. Can anyone guess what it does? :)

totalWords = 0
Dir["/chuck/drupal-pot/*.pot"].each do |fileName|
  fileWords = 0
  contents = File.read("#{fileName}")
  contents.scan(/msgid "(.+)"/) do |x| 
    line = x.join.gsub(/<\/?[^>]*>/, "")
    line.gsub!(/[%&\\\/]\w+/, "")
    line.scan(/[\w-]+/) do |y| 
      fileWords += 1
    end 
  end
  print "#{fileWords}: #{fileName}\n"
  totalWords += fileWords
end
print "#{totalWords} words"