Skip to content

Instantly share code, notes, and snippets.

@alexeyzab
Created June 19, 2015 23:55
Show Gist options
  • Save alexeyzab/d99b6a72889aa2743cad to your computer and use it in GitHub Desktop.
Save alexeyzab/d99b6a72889aa2743cad to your computer and use it in GitHub Desktop.
Caesar cipher in Ruby
def caesar_cipher(input, shift)
positions = input.unpack("C*")
shifted_positions = positions.map do |pos|
case pos
when (65..90), (97..122)
shifted = pos + (shift % 26)
if (shifted > 90 && shifted < 97) || (shifted > 122)
shifted = shifted - 26
end
pos = shifted
else
pos
end
end
shifted_positions.pack("C*")
end
puts caesar_cipher("Fraq hf gur pbqr lbh hfrq gb qrpbqr guvf zrffntr", 13)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment