Skip to content

Instantly share code, notes, and snippets.

@erbesharat
Created October 6, 2016 11:03
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 erbesharat/a0b717918729f09f1a7b5a69eafbc41c to your computer and use it in GitHub Desktop.
Save erbesharat/a0b717918729f09f1a7b5a69eafbc41c to your computer and use it in GitHub Desktop.
Caesar cipher - Ruby
def caesar(text,key)
asc = text.chars.map { |c| c.ord}
shifted = asc.map { |c| c + key.to_i}
return shifted.map { |c| c.chr }.join
end
puts caesar("Erfan", 5) # Should be --> Jwkfs
puts caesar("Ruby", 3) # Should be --> Uxe|
puts caesar("Rails", 2) # Should be --> Tcknu
puts caesar("Golang", 8) # Should be --> Owtivo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment