Skip to content

Instantly share code, notes, and snippets.

@AlexanderFisenko
Created July 13, 2015 08:11
Show Gist options
  • Save AlexanderFisenko/ec89de6d9cd0296f062f to your computer and use it in GitHub Desktop.
Save AlexanderFisenko/ec89de6d9cd0296f062f to your computer and use it in GitHub Desktop.

Revisions

  1. AlexanderFisenko created this gist Jul 13, 2015.
    21 changes: 21 additions & 0 deletions rot13.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    def rot13(string)
    a = 'a'.ord
    z = 'z'.ord
    capital_a = 'A'.ord
    capital_z = 'Z'.ord
    decyphered_string = ""

    string.unpack('c*').each do |char|
    if (a..z).cover?(char)
    modified_char = (((char - a) + 13) % 26) + a
    decyphered_string << [modified_char].pack('c*')
    elsif (capital_a..capital_z).cover?(char)
    modified_char = (((char - capital_a) + 13) % 26) + capital_a
    decyphered_string << [modified_char].pack('c*')
    else
    decyphered_string << char
    end
    end

    decyphered_string
    end