implement caesar cipher

Home   »   implement caesar cipher

def transpose(message, mapping)
  message.chars.map {|e| mapping[e]}.join
end

puts "encode or decode?"
selected_action = gets.chomp
puts "what is the key?"
key = gets.chomp
puts "what is your message?"
message = gets.chomp

plain = ('a'..'z').to_a
rotated = plain.rotate(key.to_i)

encode_mapping = plain.zip(rotated).to_h.tap {|h| h[" "] = " "}

actions = {
  "encode" => encode_mapping,
  "decode" => encode_mapping.invert
}

transposed_message = transpose(message, actions[selected_action])

puts "this is your modified message: #{transposed_message}"

Leave a Reply

Your email address will not be published.