2017-09-23 10:01:46 +02:00
|
|
|
class String
|
|
|
|
|
ALFABET = ("A".."Z").to_a
|
2013-04-10 12:38:42 -07:00
|
|
|
|
2017-09-23 10:01:46 +02:00
|
|
|
def caesar_cipher(num)
|
|
|
|
|
self.tr(ALFABET.join, ALFABET.rotate(num).join)
|
2013-10-27 22:24:23 +00:00
|
|
|
end
|
2013-04-10 12:38:42 -07:00
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
2017-09-23 10:01:46 +02:00
|
|
|
#demo:
|
|
|
|
|
encypted = "THEYBROKEOURCIPHEREVERYONECANREADTHIS".caesar_cipher(3)
|
|
|
|
|
decrypted = encypted.caesar_cipher(-3)
|