Skip to content

Instantly share code, notes, and snippets.

@everylittlefox
Created August 6, 2022 13:26
Show Gist options
  • Select an option

  • Save everylittlefox/e9733fa10ec106daa404826f34cc3a68 to your computer and use it in GitHub Desktop.

Select an option

Save everylittlefox/e9733fa10ec106daa404826f34cc3a68 to your computer and use it in GitHub Desktop.
def is_upcase?(string)
string.upcase == string
end
def caesar_cipher(string, key)
upper = "A".ord
lower = "a".ord
cipher_arr = string.chars.map do |c|
if "a".upto("z").include?(c.downcase)
letter_case = is_upcase?(c) ? upper : lower
diff = (c.ord - letter_case + key) % 26
mapped_value = letter_case + diff
mapped_value.chr
else
c
end
end
cipher_arr.join
end
p caesar_cipher("What a string!", 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment