From: Xavier Noria Date: 2003-09-17T00:00:56+09:00 Subject: Re: scrambler one-liner On Tuesday 16 September 2003 16:50, Weirich, James wrote: > > -pe > > 'gsub!(/\w+/){|w|r=1..w.size-1;w[r]=w[r].split(//).sort_by{rand}.to_s >;w}' > > Hmmm ... This seems to smbrcale all the lertets but the fsrit. It > souhld lvaee the last ctaeachrr in pclae. > > Try: > > ruby -pe > 'gsub!(/\w+/){|w|(w.size>1)?(r=1...w.size-1;w[r]=w[r].split(//).sort_ >by{rand }.to_s;w):w}' > > It's leongr, but it gevis the rihgt asnwer. Oh yes, I got size-1 wrong: -pe 'gsub!(/\w+/){|w|r=1..w.size-2;w[r]=w[r].split(//).sort_by{rand}.to_s;w}' It seems there is no need to treat the case w.size > 1 specially because the slice gives an empty string for sizes == 1 or 2 and everything seems to work right AFAICT. -- fxn