Skip to content

Instantly share code, notes, and snippets.

@vinhboy
Created January 25, 2012 09:01
Show Gist options
  • Save vinhboy/1675518 to your computer and use it in GitHub Desktop.
Save vinhboy/1675518 to your computer and use it in GitHub Desktop.
# 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