Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy ROT13 Ruby "programme" mystery

Tags:

ruby

rot13

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?

like image 396
Pavol Avatar asked May 14 '26 05:05

Pavol


1 Answers

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")

like image 104
genkilabs Avatar answered May 15 '26 23:05

genkilabs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!