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.
Mike (Guest)
on 2006-04-23 19:00
(Received via mailing list)
This is my first Ruby Q. 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 Q. #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
Logan C. (Guest)
on 2006-04-23 19: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
Dave B. (Guest)
on 2006-04-23 20: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.
James G. (Guest)
on 2006-04-23 23:39
(Received via mailing list)
On Apr 23, 2006, at 11:20 AM, Dave B. wrote:

> It won't shuffle letters

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

James Edward G. II
Dave B. (Guest)
on 2006-04-24 02:08
(Received via mailing list)
On 4/24/06, James Edward G. II <removed_email_address@domain.invalid> wrote:
> On Apr 23, 2006, at 11:20 AM, Dave B. 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.