Caesar Cipher

Kazym Omoniyi
2 min readFeb 7, 2021


In this post I intend to explain my implementation of the popular Caesar’s cipher.This is a kind of simple encryption used in the past by Caesar to communicate with his officials.

I am currently going through the Odin Project Full Stack curriculum and Caesar Cipher happens to be the first project in the basic Ruby project section.

The Caesar Cipher is decoded by shifting each letter in the given word or phrase down the alphabet by a specified number.Let us assume our given word is “abc” and specified number is 2. Decoding this will give us “cde” because we have shifted each letter 2 positions down the English alphabet.

Below is my code solution in Ruby language.

Ruby Code

Code Explanation

This solution is just a function named caesar_cipher with two arguments given_string and num as seen in line 1.As expected,one has to come up with his plan to solve any problem paying attention to what the inputs are and as such what data structures are needed.

So line 4 to 7 shows the declaration of variables to hold the input and expected output.The given string is made into an array of individual string or letter.For example the string “abc” becomes [“a”,“b”,“c”].We also have two arrays of upper and lower case letters. The empty string declared in line 7 is to hold our output.

Line 9 to 21 of our code displays the core of this implementation.Here we check each string/letter contained in our given string that we have converted into an array if it exists in the alphabet arrays.This is a kind of loop implemented using the enumerable method each.If string/letter is found its position is shifted by num and stored in our new_string variable.And if not found it is stored as is without shifting.

Note that the modulo part (%) is so as to help us return the letter at position 26 or greater. After checking all strings/letters at the termination of the loop,the new_string variable containing the decoded string by Caesar is printed.

That is all about this simple implementation of Caesar cipher.

Thank you for reading.



Kazym Omoniyi

Radio Network Optimization Engineer and Aspiring Fullstack Developer.I blog about my learning at