Skip to content

Instantly share code, notes, and snippets.

@vanceingalls
Last active December 28, 2015 19:49
Show Gist options
  • Save vanceingalls/7552589 to your computer and use it in GitHub Desktop.
Save vanceingalls/7552589 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
position = alpha.index(lower)
i +=1
if position.nil?
newStr += char
next
end
# will loop to start
if position + x > alpha.length
letter = alpha[x - (alpha.length - position)]
# will remain within limit
else
letter = alpha[position + 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