Skip to content

Instantly share code, notes, and snippets.

@avanishgiri
Last active December 28, 2015 23:09
Show Gist options
  • Save avanishgiri/7576301 to your computer and use it in GitHub Desktop.
Save avanishgiri/7576301 to your computer and use it in GitHub Desktop.
def is_lower(c)
c.ord >= 'a'.ord && c.ord <= 'z'.ord
end
def is_upper(c)
c.ord >= 'A'.ord && c.ord <= 'Z'.ord
end
def rotx(x, string, encrypt = true)
x = encrypt ? x : -x
string.each_char.map do |c|
if is_lower(c)
delta = 'a'.ord
elsif is_upper(c)
delta = 'A'.ord
else
next c
end
(((c.ord - delta + x) % 26) + delta).chr
end.join
end
p rotx(10,(p rotx(10, 'Hello World')),false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment