Created
November 19, 2013 21:28
-
-
Save vanceingalls/7552907 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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