From: "David A. Black" Date: 2003-09-18T01:57:46+09:00 Subject: Re: scrambler one-liner Hi -- On Thu, 18 Sep 2003, Peter wrote: > > def reorder(str) > > result = [] > > while str.length > 0 > > result << str.delete_at(rand(str.length)) > > end > > result > > end > > > > def scramble(str) > > str.gsub!(/\B\w+\B/){reorder($&.split(//)).join} > > end > > Here's a one-liner after all: > > gsub!(/\B\w+\B/){proc{|s|Array.new(s.length){s.delete_at(rand(s.length))}}.call($&.split(//)).join} > > It's longer for sure, but now you're sure the result is really random > (provided you believe me it is ;-) Inspired by the rand thing, I came up with: gsub!(/\B\w+\B/){|s|Array.new(s.size){s.slice!(rand(s.size)).chr}.join} I leave it to the rand experts to tell me if this is really rand. I *think* it is, assuming that the first s.size gets evaluated once and the second one gets evaluated each time through the loop. David -- David A. Black dblack@wobblini.net