Skip to content

Instantly share code, notes, and snippets.

@osazemeu
Forked from matugm/caesar.rb
Created June 29, 2016 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osazemeu/e298b0db7a171c99a86c5b3ce9407ec9 to your computer and use it in GitHub Desktop.
Save osazemeu/e298b0db7a171c99a86c5b3ce9407ec9 to your computer and use it in GitHub Desktop.
Caesar cipher using Ruby
ALPHABET_SIZE = 26
def caesar_cipher(string)
shiftyArray = []
charLine = string.chars.map(&:ord)
shift = 1
ALPHABET_SIZE.times do |shift|
shiftyArray << charLine.map do |c|
((c + shift) < 123 ? (c + shift) : (c + shift) - 26).chr
end.join
end
shiftyArray
end
puts caesar_cipher("testing")
def caesar_cipher(string, shift = 1)
alphabet = Array('a'..'z')
encrypter = Hash[alphabet.zip(alphabet.rotate(shift))]
string.chars.map { |c| encrypter.fetch(c, " ") }
end
p caesar_cipher("testing").join
def caesar_cipher(string, shift = 1)
alphabet = Array('a'..'z')
non_caps = Hash[alphabet.zip(alphabet.rotate(shift))]
alphabet = Array('A'..'Z')
caps = Hash[alphabet.zip(alphabet.rotate(shift))]
encrypter = non_caps.merge(caps)
string.chars.map { |c| encrypter.fetch(c, c) }
end
p caesar_cipher("testingzZ1Z").join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment