1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Generating Cryptograms With Ruby


4 of 5

Moving Toward a Cryptogram

All of the tricky parts of the script are now over. The crypt_table hash now contains a lookup table for all of the letters. All that's left is to print the cryptogram. A title is printed, then a "working space" for people solving the puzzle.

Part 4: Using the ARGF Variable

The final part of the script uses the ARGF special variable. The ARGF variable is a file handle that will open each of the files listed on the command line in order. For example, if you were to run the script as ./crytogram.rb file1 file2 file3, using this special variable would open each of the files listed on the command line in turn.

Each character in each of the files listed is read. If it has a corresponding key in the crypt_table hash, a lookup is performed and the crypted letter is printed. Otherwise, the character itself is printed. Since the crypt_table hash doesn't contain characters like punctuation or spaces, those characters pass through unaltered.

  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Ruby Scripts--Cryptograms

©2014 About.com. All rights reserved.