# \[SOLUTION\] Text Munger #76

**URL:** <https://www.ruby-forum.com/t/solution-text-munger-76/58580>\
**Category:** Ruby\
**Created:** [April 23, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/solution-text-munger-76/58580 "2006-04-23T17:00:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [April 23, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/solution-text-munger-76/58580/1 "2006-04-23T17:00:07Z")

</div>

This is my first Ruby Q. submission as well (there seems to be a lot  
of us  
this time through!). I’m going to attach the file because one line is  
crazy  
long and I don’t want text wrapping to be a problem 😉

I was able to squeeze things down to 15 lines in the end.

# Ruby Q. #76 - Text Munger by Michael Brum

# 

# Usage: ruby rq76-munger.rb text\_file.txt

# 

# It seems to me that once a word gets past a certain length

# the readability of that word once munged drops conciderably.

# In trying to take that into account, for words over 8 characters

# in length, I split the middle section into two strings and munge

# those separately.

def munge(word)  
case word.length  
when 0…3: return word  
when 4…8: return word[0].chr +  
word[1,(word.length-2)].split(//).to\_a.sort\_by{rand}.to\_s +  
word[word.length-1].chr  
else return word[0].chr +  
word[1,(word.length/2)].split(//).to\_a.sort\_by{rand}.to\_s +  
word[(word.length/2),(word.length-2)].split(//).to\_a.sort\_by{rand}.to\_s  
+  
word[(word.length-1)].chr  
end  
end

mtext = String.new()  
File.open(ARGV[0]) do |file|  
line = file.gets(separator=nil)  
line.split(/([^A-Za-z])/).each do |word|  
mtext += munge(word)  
end  
puts mtext  
end

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [April 23, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/solution-text-munger-76/58580/2 "2006-04-23T17:40:33Z")

</div>

Here’s mine, it doesn’t do nifty accented characters, but on the  
other hand at least numbers and underscores aren’t letters  
usage: ruby scramble.rb \< input.txt

% cat scramble.rb  
STDIN.read.gsub(/(\b[A-Za-z])([A-Za-z’]\*)([A-Za-z])/){[$1,$3,$2.split  
(//).sort\_by{rand}.join].values\_at(0,2,1).join}.display

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [April 23, 2006, 6:23pm UTC](https://www.ruby-forum.com/t/solution-text-munger-76/58580/3 "2006-04-23T18:23:22Z")

</div>

My version features an un-munging mode and the use of procs and blocks  
to keep DRY. It won’t shuffle letters, but it will leave numbers and  
underscores alone, too.

[http://www.dave.burt.id.au/ruby/text\_munger.rb](http://www.dave.burt.id.au/ruby/text_munger.rb)

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [April 23, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/solution-text-munger-76/58580/4 "2006-04-23T21:39:59Z")

</div>

On Apr 23, 2006, at 11:20 AM, Dave B. wrote:

> It won’t shuffle letters

Is there a missing word in there? (I hope…) 😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [April 24, 2006, 12:08am UTC](https://www.ruby-forum.com/t/solution-text-munger-76/58580/5 "2006-04-24T00:08:19Z")

</div>

On 4/24/06, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Apr 23, 2006, at 11:20 AM, Dave B. wrote:
> 
> > It won’t shuffle letters
> 
> Is there a missing word in there? (I hope…) 😉

It won’t shuffle _accented_ letters!

And, Gordon Thiesfield, very nice!

Cheers,  
Dave
