O'Reilly logo

Ruby by Example by Kevin C. Baird

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

#12 Counting Words in a File (word_count.rb)

It’s often handy to know the number of words in a file. Word count is a standard feature in word-processing programs, but if you’re not using a word processor, obtaining a word count might not be so easy. I originally wrote this script when I was working on a project using an XML-based document production system called DocBook (http://www.docbook.org) and wanted to have a word count that roughly corresponded to those you could get from a word processor. The Unix command wc counts words, but the numbers it reported didn’t necessarily match what a word processor might report; the main reason probably had to do with issues like whether words with fewer than a certain number of letters should count as a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required