Created
January 25, 2012 09:01
-
-
Save vinhboy/1675518 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# the last letter position | |
length = input.length - 1 | |
# the first letter | |
first = input[0] | |
# the lat letter | |
last = input[length] | |
# replace the first letter with the last | |
input[0] = last | |
# replace the last letter with the first | |
input[length] = first | |
OR ---------- use regex | |
input.gsub!(/(^\S)(.*)(\S$)/,'\3\2\1') | |
(^\S) - first group - first letter | |
(.*) - second group - all letters | |
(\S$) - third group - last letter | |
\3\2\1 - show third group, second group, first group | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment