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

Generating Cryptograms With Ruby


2 of 5

Generating the alpha Array

The range operator .. is used to create a sequence of strings from 'a' to 'z'. This range is then expanded into an array, so the alpha array now contains single-character strings for each of the letters in the alphabet.

The alpha array is then sorted randomly. While newer versions of Ruby have a shuffle method for randomizing arrays, not everyone is using these versions yet, so sorting an array with the rand method is an acceptable alternative. The randomized alphabet is then stored in the crypt variable.

Finally, the alpha and crypt are mapped to their capitalized counterparts and stored in alpha_caps and crypt_caps.

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

©2014 About.com. All rights reserved.