From: STEPHEN BECKER I V Date: 2004-09-28T09:23:32+09:00 Subject: Re: {newb} Each statements ven.rb:27:in `encrypt': undefined method `-' for "a":String (NoMethodError) from ven.rb:27:in `map' from ven.rb:27:in `encrypt' from ven.rb:38 still ############33here is my code complete its some cipher def decrypt(data) ndata=data.clone srand(5000) a = ndata.size-1 # control loop while a>-1 temp=ndata[a] temp-=rand(10) temp= 26-((temp.abs)%26) if temp<0 temp= temp%26 if temp>-1 ndata[a]=temp+97 a-=1 end return ndata end def encrypt(data) ndata=data.dup a = ndata.size-1 srand(5000) ndata = ndata.split(//).map{ |c| ((c-97)+rand(10))%26 }.join #suggested code #end # this code down here works but its to messy # while a>-1 # ndata[a]= ((ndata[a]-97)+rand(10))%26 # a-=1 #end return ndata end it=String.new("abcdefghijklmnop") print decrypt(encrypt(it)) ################### and i get the error above with the new added code. Sorry to be such a pain. Becker On Tue, 28 Sep 2004 05:41:53 +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. > > Regards, > > Brian > > STEPHEN BECKER I V wrote: > > nope > > > > > > Z:/ruby/vernam.rb:26:in `encrypt': undefined method `map!' for > > "abcdefghijklmnop":String (NoMethodE > > rror) > > from Z:/ruby/vernam.rb:38 > > Press any key to continue . . . > > > > > > On Tue, 28 Sep 2004 05:22:54 +0900, James Edward Gray II > > wrote: > > > >> > >>On Sep 27, 2004, at 3:13 PM, STEPHEN BECKER I V wrote: > >> > >> > >>>Do each statements change the thing that they are using? > >>>data is a a string. > >>>##code > >>> > >>> ndata=data.clone > >>> a = ndata.size-1 > >>> srand(5000) > >>> ndata.each do |c| > >>> c=((c-97)+rand(10))%26 > >>> end > >>>## code > >>> > >>>So ndata indexes are not change? Am i doing this wrong (well i know > >>>its not working so it cant be right.)? I have it working with a while > >>>loop but i am trying to teach my self the ruby way. > >> > >>How about using: > >> > >>ndata.map! do |c| > >> ((c-97)+rand(10))%26 > >>end > > > -- > Brian Schr�der > http://ruby.brian-schroeder.de/ > >