[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

{newb} Each statements

STEPHEN BECKER I V

9/27/2004 8:14:00 PM

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.

Stephen


14 Answers

Austin Ziegler

9/27/2004 8:21:00 PM

0

Try using #map instead.

On Tue, 28 Sep 2004 05:13:30 +0900, STEPHEN BECKER I V
<becker004@gmail.com> wrote:
> Do each statements change the thing that they are using?
> data is a a string.

ndata=data.dup # dup is probably what you want here
a = ndata.size-1
srand(5000)
ndata.map { |c| c=((c-97)+rand(10))%26 }

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca
: as of this email, I have [ 6 ] Gmail invitations


James Gray

9/27/2004 8:23:00 PM

0

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

Does that do it?

James Edward Gray II



STEPHEN BECKER I V

9/27/2004 8:30:00 PM

0

data is a string so ndata is a string as well correct? i am getting an error

"Z:/ruby/vernam.rb:26:in `encrypt': undefined method `-' for
"abcdefghijklmnop":String (NoMethodErro
r)
from Z:/ruby/vernam.rb:26:in `map'
from Z:/ruby/vernam.rb:26:in `each'
from Z:/ruby/vernam.rb:26:in `map'
from Z:/ruby/vernam.rb:26:in `encrypt'
from Z:/ruby/vernam.rb:36"


STEPHEN BECKER I V

9/27/2004 8:34:00 PM

0

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
<james@grayproductions.net> 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
>
> Does that do it?
>
> James Edward Gray II
>
>


Brian Schröder

9/27/2004 8:42:00 PM

0

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
> <james@grayproductions.net> 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-sch...


Bill Guindon

9/27/2004 8:56:00 PM

0

On Tue, 28 Sep 2004 05:13:30 +0900, STEPHEN BECKER I V
<becker004@gmail.com> wrote:
> Do each statements change the thing that they are using?
> data is a a string.

no, but map! and collect! do, but they only work on Arrays, not
Strings. So first, you'll need to split the string into an Array...
for strings, each_byte will do that.

srand(5000)
ndata = ''
a = data.size-1
data.each_byte do |c|
ndata += (((c-97)+rand(10))%26).chr
end

If you're looking for a random human readable string, you'll want to
bump it back up by 96 as you go along...
ndata += (((c-97)+rand(10))%26 + 97).chr


--
Bill Guindon (aka aGorilla)


dblack

9/27/2004 9:07:00 PM

0

Bill Guindon

9/27/2004 9:26:00 PM

0

On Tue, 28 Sep 2004 06:07:01 +0900, David A. Black <dblack@wobblini.net> wrote:

> On Tue, 28 Sep 2004, Bill Guindon wrote:

> > srand(5000)
> > ndata = ''
> > a = data.size-1
> > data.each_byte do |c|
> > ndata += (((c-97)+rand(10))%26).chr
> > end
>
> That's a bit inefficient, because each time through you're creating a
> new string in ndata. You could use << to append instead.

hmmm... hadn't thought of that. Still have some old habits to break.
/me takes notes.

--
Bill Guindon (aka aGorilla)


STEPHEN BECKER I V

9/28/2004 12:24:00 AM

0

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
<ruby@brian-schroeder.de> 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
> > <james@grayproductions.net> 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-sch...
>
>



James Gray

9/28/2004 12:33:00 AM

0

On Sep 27, 2004, at 7:23 PM, STEPHEN BECKER I V wrote:

> 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

split() takes one String and makes many out of it. You are trying to
use those Strings as characters. See comment below...

> ############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

ndata = ndata.split(//).map{ |c| ((c[0]-97)+rand(10))%26 }.join

> 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))
> ###################

Hope that helps.

James Edward Gray II