1. Computing
Send to a Friend via Email

Generating Cryptograms With Ruby

By

5 of 5

The Script
#!/usr/bin/env ruby

# PART 1
# Alphabet and randomized alphabet
alpha = ('a'..'z').to_a
crypt = alpha.sort { rand(3) - 1 }

# The same in capital letters
alpha_caps = alpha.map{|c| c.upcase }
crypt_caps = crypt.map{|c| c.upcase }

# PART 2
# "Zip" them together and flatten to create a hash
crypt_table = Hash[*
  (alpha.zip(crypt) + alpha_caps.zip(crypt_caps)).flatten
]

# PART 3
# Title
puts "Cryptogram"
puts

# Working area
puts alpha.join(' ')
puts alpha.map{|c| '_'}.join(' ')
puts

# PART 4
# Puzzle
ARGF.each_byte do|c|
  c = c.chr
  print crypt_table.has_key?(c) ? crypt_table[c] : c
end
 

©2014 About.com. All rights reserved.