Today’s goal is to boot up your familiarity with Ruby via largely self-directed exploration. Picking up a language like this is a good skill! Since Ruby is popular for web development (and hence widely documented online), this should be pretty easy.

Resources

See intructions for installing or using ruby and irb, an interpreter and a REPL for the Ruby language. There is minor configuration to do on the CS Linux machines (add one line to a configuration file). There are also instructions for installing on your own machine.

In addition to notes from this course (focused more on a few specific features), use the online Ruby documentation at http://ruby-doc.org/ to look up standard library features or language features.

A quick tour of Arrays, Hashes, and Ranges

There is good documentation of the core language itself here, although for some reason I cannot find a page that links to each of these pieces in a pretty way.

The original version of the Programming Ruby book is available online. (Newer versions $) It covers an older version of the language. Most of the language remains the same – there are some small differences, especially with respect to scope of local variables and blocks.

Since Ruby is a popular language for web development, there are numerous quick-learn online resources including CodeAcademy and RubyMonds to get you familiar with core syntax and idioms.

Word Frequency Counter

Build a word frequency counter using Ruby’s built-in Hashes, Arays, File support, etc.

Once you have a wordfreq.rb file, you can run it with ruby wordreq.rb [arguments ..]. When running a program this way, you can access its command line arguemnts (an array of strings) through the global variable ARGV. Or you can launch irb and use load "wordfreq.rb" much like use worked in sml. In the latter case, ARGV will not be useful.

Reading the entire contents of a file and splitting it into an Array of space-delimited words is as easy as File.read("filename.txt").split, although this is not a very efficient way, since it reads the entire file as a string before splitting it.

Use Ruby’s blocks and the built-in methods of Arrays and Hashes to count the frequency of each word that appears in a given file. Refer to documentation often!

The organization of your code into methods is up to you. You are not required to define any classes.

Write methods to accomplish the following tasks for a file (checking the library documentation for Array or Hash will make some of these very easy!):

  • Count the total number of distinct words appearing.
  • List the words that appear only once.
  • List the words that appear more than n times, for a given n.
  • Rank the words by number of occurences.

Here are some test inputs: * alchemy.txt * hamlet.txt