Text Munger (#76) simplistic solution


#1

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


#2

“A LeDonne” removed_email_address@domain.invalid writes:

This solution makes no attempt to deal with non-ascii, AND counts
underscore as a word character.

… and niheter deos mine.

Now, taht was a nice, sorht quiz good for a qucik barek… I knew the

poerblm came up brfeoe, but I wrote my sotouiln berfoe loknoig at

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/82166.

STDIN.each { |line| puts line.gsub(/\B\w+\B/) {
$&.split(’’).sort_by{rand} } }

Eojny.