Skip to content

Instantly share code, notes, and snippets.

@vanceingalls
Created November 19, 2013 21:28
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 vanceingalls/7552907 to your computer and use it in GitHub Desktop.
Save vanceingalls/7552907 to your computer and use it in GitHub Desktop.
def rotx(x, string, encrypt=true)
i = 0
alpha = 'abcdefghijklmnopqrstuvwxyz'
newStr = ''
alpha.reverse! unless encrypt
# convert to one cycle
x = x > 26 ? x % 26 : x
while i < string.length do
char = string[i]
isCap = (char =~ /[A-Z]/) == 0
lower = char.downcase
ind = alpha.index(lower)
i +=1
if ind.nil?
newStr += char
next
end
# will loop to start
if ind + x > alpha.length
letter = alpha[x - (alpha.length - ind)]
# will remain within limit
else
letter = alpha[ind + x]
end
# check capitaliztion
newStr += isCap ? letter.upcase : letter
end
puts newStr
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment