Last active
March 2, 2018 12:05
-
-
Save yoonwaiyan/ad590dad4bb43aaca2a42a4efbf7a1f6 to your computer and use it in GitHub Desktop.
Caesar Cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CaesarCipher | |
def initialize(shift) | |
@shift = shift | |
end | |
def call(input) | |
[capital_characters, small_characters].each do |chars| | |
input = input.tr(chars.join, encrypt(chars).join) | |
end | |
input | |
end | |
private | |
def caesar_shift(char_index) | |
(char_index - @shift) % 26 | |
end | |
def capital_characters | |
('A'..'Z').to_a | |
end | |
def small_characters | |
('a'..'z').to_a | |
end | |
def encrypt(chars) | |
chars.each_with_index.map do |c, char_index| | |
chars[caesar_shift(char_index)] | |
end | |
end | |
end | |
puts CaesarCipher.new(3).("This is so interesting") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment