# Text Munger (#76) simplistic solution

**URL:** <https://www.ruby-forum.com/t/text-munger-76-simplistic-solution/58578>\
**Category:** Ruby\
**Created:** [April 23, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/text-munger-76-simplistic-solution/58578 "2006-04-23T16:47:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_LeDonne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6bbea6/32.png) [@A\_LeDonne](https://www.ruby-forum.com/u/A_LeDonne)\
**Post date:** [April 23, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/text-munger-76-simplistic-solution/58578/1 "2006-04-23T16:47:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![A\_LeDonne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6bbea6/32.png) [@A\_LeDonne](https://www.ruby-forum.com/u/A_LeDonne)\
**Post date:** [April 23, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/text-munger-76-simplistic-solution/58578/2 "2006-04-23T17:31:01Z")

</div>

“A LeDonne” [removed\_email\_address@domain.invalid](mailto: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](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.
