Skip to content

Instantly share code, notes, and snippets.

@yoonwaiyan
Last active March 2, 2018 12:05
Show Gist options
  • Save yoonwaiyan/ad590dad4bb43aaca2a42a4efbf7a1f6 to your computer and use it in GitHub Desktop.
Save yoonwaiyan/ad590dad4bb43aaca2a42a4efbf7a1f6 to your computer and use it in GitHub Desktop.
Caesar Cipher
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