I made a simple ROT13 programme and I don't understand one thing:
a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
(a.length+1).times do |i|
print a[i + 13]
if i>13
print a[i %14]
end
end
Outputs:
NOPQRSTUVWXYZABCDEFGHIJKLM
If I don't add +1 after a.length, the iteration ends with the letter L. However, if I use print a[i] inside the iteration, it normally starts with A and ends with Z with no +1 addition needed.
Can someone explain this mystery for me?
I just needed a quick rot13 one liner and this SO entry was the first google result. I kept on searching and found a super small one by @xfaider that worked well enough for my purpose.
Just gonna post it here for the next person who wants a one-liner.
string.tr("A-Za-z", "N-ZA-Mn-za-m")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With