Skip to content

Instantly share code, notes, and snippets.

@MBM1607
Created February 25, 2021 06:00
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 MBM1607/25fab91a68ddc3af3311868ea20d3a80 to your computer and use it in GitHub Desktop.
Save MBM1607/25fab91a68ddc3af3311868ea20d3a80 to your computer and use it in GitHub Desktop.
Odin Project Ruby Ceaser Cipher
def caesar_cipher(message, shift)
split_message = message.split("").map do |letter|
# If letter is not an alphabet do not change it
if letter.downcase.ord < 97 or letter.downcase.ord > 128
letter
else
code = letter.ord + shift
if (letter == letter.upcase && code > 90) || (letter == letter.downcase && code > 122)
code -= 26
end
code.chr
end
end
return split_message.join("")
end
puts caesar_cipher("What a string!", 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment