# \[solution\]

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

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 24, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/solution/94191/1 "2006-04-24T22:29:39Z")

</div>

I’m a fairly lousy scripter at best, but finally saw a Ruby Q. I could  
manage.

That said, this probably wins the prize for “slowest, most convoluted  
way of doing it”… but it’s how I think, I’m afraid…

Usage: it extends String, so foo = bar.munge

Please don’t laugh 🙂

* * *

class String  
def mangle  
if self.size \< 3  
self  
else  
original = self.split(//)  
first = self[0,1]  
last = self[-1,1]  
innards\_munged = self[1,(size-2)].gsub(/[^a-zA-Z]/,  
“”).shuffle!.split(//)

```
  output = []

  output << first

  original.each { |l|
    if l =~ /[a-zA-Z]/
      output << innards_munged.pop
    else
      output << l
    end
  }
  output << last
  output.to_s
end

```

end

def shuffle!  
(0…size).each { |j|  
i = rand(size-j)  
self[j], self[j+i] = self[j+i], self[j]  
}  
self  
end

def munge  
words = self.split(/\b/)  
output = []  
words.each { |w|  
output \<\< w.mangle  
}  
output.to\_s  
end  
end

* * *

---

<div class="post-metadata">

**Author:** ![tma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ed655f/32.png) [@tma](https://www.ruby-forum.com/u/tma)\
**Post date:** [April 25, 2006, 1:07am UTC](https://www.ruby-forum.com/t/solution/94191/2 "2006-04-25T01:07:16Z")

</div>

On Apr 24, 2006, at 3:26 PM, Tom A. wrote:

> I’m a fairly lousy scripter at best, but finally saw a Ruby Q. I  
> could manage.

But now that you have worked with the problem you can read the  
solutions of others and learn all kinds of new things… 😉 Welcome  
to Ruby Q.!

James Edward G. II
