Last active
December 28, 2015 19:49
-
-
Save vanceingalls/7552589 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 | |
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