From: Brian Candler Date: 2004-09-28T20:36:55+09:00 Subject: Re: {newb} Each statements On Tue, Sep 28, 2004 at 05:41:53AM +0900, Brian Schr?der wrote: > ndata = ndata.split(//).map { | c | ((c-97)+rand(10))%26 }.join > > But there was a thread the efficency of the different ways to loop the > bytes of a string some time ago. > > What I'm doing is not inplace, and I'm creating two now objects on the > way, so if efficency is important this is not perfect. gsub! is another option here: ndata.gsub!(/./) { |c| ((c[0]-97+rand(10))%26+97).chr } I think it's a clearer way of iterating the characters of a string, and it has the advantage that you can skip characters you're not interested in, e.g. ndata.gsub!(/\w/) ... etc I'd also imagine it's more efficient than splitting the string into an array and then joining the bits back together again. Regards, Brian.