class Crypt::Rot13

The Rot13 class encapsulates methods governing character rotation.

Constants

VERSION

The version of the crypt-rot13 library.

Public Class Methods

new(str='', degree=13) click to toggle source

Returns a new Rot13 object. The object is a string with the letters each rotated by degree.

You cannot use a multiple of 26 as the degree or a Rot13::Error will be raised. So, your days of double rot13 encryption are over.

Calls superclass method
# File lib/crypt/rot13.rb, line 19
def initialize(str='', degree=13)
  str = rotate_string(str, degree) unless str.empty?
  super(str)
end

Public Instance Methods

rotate(degree) click to toggle source

Rotates the Crypt::Rot13 object by degree.

# File lib/crypt/rot13.rb, line 26
def rotate(degree)
  rotate_string(self, degree)
end

Private Instance Methods

rotate_string(str, degree) click to toggle source
# File lib/crypt/rot13.rb, line 32
def rotate_string(str, degree)
  case degree.modulo(26)
    when 0
      raise Error, 'degree must not be a multiple of 26'
    when 13
      str = str.tr('a-zA-Z', 'n-za-mN-ZA-M')
    else
      str = str.unpack('C' * str.length).map!{ |e|
        if e >= 97 && e <= 122
          e = ((e - 97 + degree) % 26) + 97
        elsif e >= 65 && e <= 90
          e = ((e - 65 + degree) % 26) + 65
        else
          e = e
        end
      }.pack('C' * str.length)
  end

  str
end