[solution]

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 :slight_smile:


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


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… :wink: Welcome
to Ruby Q.!

James Edward G. II