Nihongo (Japanese)

String#rot13

#!/usr/local/bin/ruby
# rot13.rb
# $Id: rot13-en.uhtml,v 1.4 2000-01-02 15:41:13+09 msato Exp $
# 1999/12/17 14:42:48 msato
# Algorithm of 'rot13' is described in Jargon File
# http://www.tuxedo.org/~esr/jargon/html/entry/rot13.html

class String
  def rot(num = 13)
    return self.split("").collect { |ch|
      if /^[a-z]$/ === ch
	((ch[0] + num - 'a'[0]) % 26 + 'a'[0]).chr
      elsif /^[A-Z]$/ === ch
	((ch[0] + num - 'A'[0]) % 26 + 'A'[0]).chr
      else
	ch
      end
    }.join("")
  end

  alias rot13 rot
end

if __FILE__ == $0
  while line = gets
    p line
    p line.rot13
  end
end


[Rabbit Mark] Feel free to mail me

sato.mshr@gmail.com