-
-
Save nerdinand/381231e85e4e2978faf2 to your computer and use it in GitHub Desktop.
Ruby rot13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# or simply: | |
'foobar'.tr 'A-Za-z','N-ZA-Mn-za-m' | |
# rot(x) | |
class String | |
def rot(num = 13) | |
return self.split('').collect do|ch| | |
if /^[a-z]$/ === ch | |
((ch.ord + num - 'a'.ord) % 26 + 'a'.ord).chr | |
elsif /^[A-Z]$/ === ch | |
((ch.ord + num - 'A'.ord) % 26 + 'A'.ord).chr | |
else | |
ch | |
end | |
end.join('') | |
end | |
alias rot13 rot | |
end | |
# Example usage | |
p 'fefe'.rot(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment