This solution makes no attempt to deal with non-ascii, AND counts
underscore as a word character. This is the first time I read a quiz,
took a crack at a solution, and found my brain spitting out "inject"
on the first attempt.
The \w{4,} is not just an optimization to skip words of 3 letters or
less; it also ensures that runs of digits or of punctuation do not get
scrambled.
I was somewhat surprised that even with a u modifier on the regex,
POSIX regex classes like [:alpha:] don't pick up the Unicode notion of
letter. Perhaps Oniguruma will do such magic.
-A
class String
def scramble
self.split(/\b/).inject(""){ |result, wordbit| result << (
wordbit.match(/\w{4,}/)?
(wordbit[0,1] << wordbit[1..-2].split(//).sort_by{ rand
}.join("") << wordbit[-1,1]) :
wordbit ) }
end
def scramble!
self.replace self.scramble
end
end