Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created January 30, 2020 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/84e32487050fad765a3894a810e747c8 to your computer and use it in GitHub Desktop.
Save JoshCheek/84e32487050fad765a3894a810e747c8 to your computer and use it in GitHub Desktop.
Rot13
def rot13(ascii_char)
ascii_offset = 65
alphabet_size = 26
full_index = ascii_char.ord - ascii_offset
cap_bit = full_index & 0b100000
half_index = full_index & 0b011111
rotated = (half_index + alphabet_size/2) % alphabet_size
(rotated + cap_bit + ascii_offset).chr
end
"Va beqre gb trarengr gur vaivgr pbqr, znxr n CBFG erdhrfg gb /ncv/vaivgr/trarengr"
.gsub /\w/, &method(:rot13)
# => "In order to generate the invite code, make a POST request to /api/invite/generate"
"Va beqre gb trarengr gur vaivgr pbqr, znxr n CBFG erdhrfg gb /ncv/vaivgr/trarengr"
.gsub(/\w/) { |c| (((c.ord-65 & 31)+13)%26 + 65 | c.ord&32).chr }
# => "In order to generate the invite code, make a POST request to /api/invite/generate"
"Va beqre gb trarengr gur vaivgr pbqr, znxr n CBFG erdhrfg gb /ncv/vaivgr/trarengr".gsub(/\w/) { |c|
n = c.ord
n -= 65
n &= 31
n += 13
n %= 26
n += 65
n |= c.ord&32
n.chr
}
# => "In order to generate the invite code, make a POST request to /api/invite/generate"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment