Skip to content

Instantly share code, notes, and snippets.

@nerdinand
Forked from rwoeber/rot13.rb
Last active December 29, 2015 14:43
Show Gist options
  • Save nerdinand/381231e85e4e2978faf2 to your computer and use it in GitHub Desktop.
Save nerdinand/381231e85e4e2978faf2 to your computer and use it in GitHub Desktop.
Ruby rot13
# 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