Some points about making your code look more like idiomatic Ruby:
Use
nil.choice = -1should be
choice = nilDon't wrap your conditional in brackets:
while(choice != 3) if(choice == 1)should be
while choice != 3 if choice == 1Instead of initializing a variable to an empty array, use
mapto convert one array directly into the new form and use that to initialize your variable:Instead of
Instead of this:Hashes != arrays, sorry. The principal still applies, even if this example isn't equivalent:#create empty hashes plain_alphabet = {} cipher_alphabet = {} "a".upto("z") {|x| plain_alphabet[x] = ((x[0] - "a"[0]) % 26) } "A".upto("Z") {|x| cipher_alphabet[x] = ((x[0] - "A"[0]) % 26) }do this:
plain_alphabet = ("a".."z").map { |x| (x[0] - "a"[0]) % 26 } cipher_alphabet = ("A".."Z").map { |x| (x[0] - "A"[0]) % 26 }And instead of this:
ciphertext = "" #fill in the ciphertext with mapping information in the caesar_map plaintext.split("").each do |c| ciphertext += $caesar_map[c] enddo this:
ciphertext = plaintext.split("").map { |c| $ceasar_map[c] }.joinUse
casewhen appropriate; rewrite your if/elsif/elsif/else as:case choice when 1: # ... when 2: # ... when 3: # ... else # ... end