Forum: Ruby [SOLUTION]

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E1c74637dbb1b765861978e4c4f6388e?d=identicon&s=25 Tom Armitage (Guest)
on 2006-04-24 22:29
(Received via mailing list)
I'm a fairly lousy scripter at best, but finally saw a Ruby Quiz 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

---
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-25 01:07
(Received via mailing list)
On Apr 24, 2006, at 3:26 PM, Tom Armitage wrote:

> I'm a fairly lousy scripter at best, but finally saw a Ruby Quiz 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 Quiz!

James Edward Gray II
This topic is locked and can not be replied to.