{newb} Each statements

S

STEPHEN BECKER I V

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
 
A

Austin Ziegler

Try using #map instead.

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
 
J

James Edward Gray II

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
 
S

STEPHEN BECKER I V

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"
 
S

STEPHEN BECKER I V

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 . . .
 
B

Brian Schröder

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
 
B

Bill Guindon

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
 
D

David A. Black

Hi --

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

That's a bit inefficient, because each time through you're creating a
new string in ndata. You could use << to append instead.


David
 
B

Bill Guindon

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.
 
S

STEPHEN BECKER I V

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
 
J

James Edward Gray II

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
 
B

Bill Guindon

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

That started to look a bit too perl like for me, so I decided to
compare the 'each_byte' and the 'split' approaches. Granted, it's a
simple comparison, but seems each_byte is a bit faster, and looks
easier to read (at least to me).

btw, suggestions on simple benchmarking welcome.

Here's my simple comparison script:
######################################
data = "some really silly string of text which will be encrypted soon"

######################################
def encrypt_byte(data)
ndata = ''
a = data.size-1
srand(5000)
data.each_byte {|c| ndata << (((c-96)+rand(10))%26) + 97}
return ndata
end

######################################
def encrypt_split(data)
ndata=data.dup
a = ndata.size-1
srand(5000)
ndata = ndata.split(//).map{ |c| (((c[0]-96)+rand(10))%26 + 97).chr}.join
end

######################################
t0 = Time.now
1000.times do
puts encrypt_byte(data)
end

t1 = Time.now
1000.times do
puts encrypt_split(data)
end

t2 = Time.now
puts t1 - t0, t2 - t1
 
D

David A. Black

Hi --

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

That started to look a bit too perl like for me, so I decided to
compare the 'each_byte' and the 'split' approaches. Granted, it's a
simple comparison, but seems each_byte is a bit faster, and looks
easier to read (at least to me).

btw, suggestions on simple benchmarking welcome.

Here's my simple comparison script:
######################################
data = "some really silly string of text which will be encrypted soon"

######################################
def encrypt_byte(data)
ndata = ''
a = data.size-1

I keep wondering what this 'a' is (I think it was in the original,
also unused).

Here's another technique you could add to the collection:

"abc".split(//).inject("") {|str,c|
str << (((c[0]-96) + rand(10)) % 26) + 97
}


David
 
S

STEPHEN BECKER I V

'a' was used for my while loop that i jimmy rigged, I dont know if it
is my decrypt, but it does not seem to decrypt it correctly.



Hi --

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

That started to look a bit too perl like for me, so I decided to
compare the 'each_byte' and the 'split' approaches. Granted, it's a
simple comparison, but seems each_byte is a bit faster, and looks
easier to read (at least to me).

btw, suggestions on simple benchmarking welcome.

Here's my simple comparison script:
######################################
data = "some really silly string of text which will be encrypted soon"

######################################
def encrypt_byte(data)
ndata = ''
a = data.size-1

I keep wondering what this 'a' is (I think it was in the original,
also unused).

Here's another technique you could add to the collection:

"abc".split(//).inject("") {|str,c|
str << (((c[0]-96) + rand(10)) % 26) + 97
}

David

--


David A. Black
(e-mail address removed)
 
B

Brian Candler

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top