Skip to content

Instantly share code, notes, and snippets.

@JoshCheek JoshCheek/rot_13.rb
Created Jan 30, 2020

Embed
What would you like to do?
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
You can’t perform that action at this time.