Forum: Ruby [SOLUTION] Text Munger #76

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Be3f276f615e29a7c605efd1d3bc3cae?d=identicon&s=25 Mike (Guest)
on 2006-04-23 17:00
(Received via mailing list)
This is my first Ruby Quiz 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
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 Quiz #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 +
    else       return word[0].chr +
word[1,(word.length/2)].split(//).to_a.sort_by{rand}.to_s +

mtext =[0]) do |file|
line = file.gets(separator=nil)
  line.split(/([^A-Za-z])/).each do |word|
    mtext += munge(word)
  puts mtext
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-04-23 17:40
(Received via mailing list)
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\b[A-Za-z])([A-Za-z']*)([A-Za-z])/){[$1,$3,$2.split
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 Dave Burt (Guest)
on 2006-04-23 18:23
(Received via mailing list)
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.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-23 21:39
(Received via mailing list)
On Apr 23, 2006, at 11:20 AM, Dave Burt wrote:

> It won't shuffle letters

Is there a missing word in there?  (I hope...)  ;)

James Edward Gray II
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 Dave Burt (Guest)
on 2006-04-24 00:08
(Received via mailing list)
On 4/24/06, James Edward Gray II <> wrote:
> On Apr 23, 2006, at 11:20 AM, Dave Burt 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!

This topic is locked and can not be replied to.