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
How about using:
ndata.map! do |c|
((c-97)+rand(10))%26
end
Does that do it?
James Edward Gray II
···
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.
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
···
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.
--
Bill Guindon (aka aGorilla)
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"
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
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-schroeder.de/
hmmm... hadn't thought of that. Still have some old habits to break.
/me takes notes.
···
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.
--
Bill Guindon (aka aGorilla)
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-schroeder.de/
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.
···
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.
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
···
On Sep 27, 2004, at 7:23 PM, STEPHEN BECKER I V wrote:
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:
···
On Tue, 28 Sep 2004 09:33:16 +0900, James Edward Gray II <james@grayproductions.net> wrote:
ndata = ndata.split(//).map{ |c| ((c[0]-97)+rand(10))%26 }.join
######################################
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
--
Bill Guindon (aka aGorilla)
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
···
On Tue, 28 Sep 2004, Bill Guindon wrote:
On Tue, 28 Sep 2004 09:33:16 +0900, James Edward Gray II > <james@grayproductions.net> wrote:
--
David A. Black
dblack@wobblini.net
'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.
···
On Tue, 28 Sep 2004 10:58:59 +0900, David A. Black <dblack@wobblini.net> wrote:
Hi --
On Tue, 28 Sep 2004, Bill Guindon wrote:
> On Tue, 28 Sep 2004 09:33:16 +0900, James Edward Gray II > > <james@grayproductions.net> wrote:
>
> > 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
dblack@wobblini.net