# \[SOLUTION\] #79 - Text Munger

**URL:** <https://www.ruby-forum.com/t/solution-79-text-munger/58697>\
**Category:** Ruby\
**Created:** [April 24, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/solution-79-text-munger/58697 "2006-04-24T22:35:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Al\_Gordon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b9bd4f/32.png) [@Al\_Gordon](https://www.ruby-forum.com/u/Al_Gordon)\
**Post date:** [April 24, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/solution-79-text-munger/58697/1 "2006-04-24T22:35:43Z")

</div>

Here’s my solution. Finally, something that even a newb like me can  
write a solution for (albeit, maybe not a great one). My solution  
feels very perl-ish to me, but, hey, it seems to work with the limited  
testing I’ve given it.

–

– AL –

#!/usr/bin/env ruby

if ! ARGV[0]  
puts “Usage: munge.rb filename.txt”  
else  
infile = File.open(ARGV[0])  
infile.map do |line|  
line.split.map do |word|  
if word =~ /\W$/  
punctuation = word[-1…-1]  
word.chop!  
else  
punctuation = “”  
end  
if word.length \> 3  
inner = word[1…-1]  
word = word[0…0] + inner.split(//).sort\_by {rand}.join(’’) +  
word[-1…-1] + punctuation  
end  
print word + " "  
end  
puts  
end  
end
