Proper way to create truly new dup/cloned object?

a = "pass"
b = a.dup
b = "fail"
puts a

fails the test. I tried with clone also. What is the proper syntax for
what I am trying to do?

···

--
Posted via http://www.ruby-forum.com/.

I’m not sure what you’re trying to accomplish with this code. When this
finishes executing, it will output "pass"; maybe you could explain why
this isn’t what you expect?

Btw. the `b' variable is entirely useless because you don’t use it for
anything.

Vale,
Marvin

···

Am 06.05.2012 12:54, schrieb ruby gem:

a = "pass"
b = a.dup
b = "fail"
puts a

fails the test. I tried with clone also. What is the proper syntax for
what I am trying to do?

it is working for me I made a typo in my original test. I don't see how
I could make a mistake with such a small test but I did, thanks.

···

--
Posted via http://www.ruby-forum.com/.

I think it is possible one of my gems is overriding the default behavior
of dup, fast_xor:

puts @l
puts cloned_l

cipher = OpenSSL::Cipher::Cipher.new 'AES-256-CTR'
cipher.encrypt
cipher.key = cloned_l.xor!(subkey)
ciphertext = cipher.update(@r)
ciphertext << cipher.final
@r = ciphertext

puts @l
puts cloned_l

output:

8bll��h�43��u3;S�/�=�
                      @QM�W
8bll��h�43��u3;S�/�=�
                      @QM�W
톭qWOʁ/@�FI`�#����//��͟}��l
톭qWOʁ/@�FI`�#����//��͟}��l

is it possible for gem to over ride dup in this way? And if so how would
you suggest going about this? my @l value changes over time , and
fast_xor only does in place operations but in some instances I need to
get l.xor! without the in place. I thought making a dup of it when I
need its current value, but don't want to change the original string,
would work. But it seems like it doesn't work because if it did it
should output this

8bll��h�43��u3;S�/�=�
                      @QM�W
8bll��h�43��u3;S�/�=�
                      @QM�W
8bll��h�43��u3;S�/�=�
                      @QM�W
톭qWOʁ/@�FI`�#����//��͟}��l

can you think of an elegant way to do this? I don't want to have to keep
adding and taking a bunch of things out of hashes but I am starting to
think that is the only option.

···

--
Posted via http://www.ruby-forum.com/.

okay -w doesn't show any warning about this so it isn't that. I have
also observed that .xor! with dup works as expected in some cases. Here
is an example showing where it works as expected

require 'xor'
require 'openssl'

@a = "aaaaaaa"
b = @a.dup
puts @a
puts b
b.xor!("bbbbbbb")
puts @a
puts b

#outputs

aaaaaaa
aaaaaaa
aaaaaaa

however, here is what I have simplified my script that I need to work to

require 'xor'
require 'openssl'

@sha256 = OpenSSL::Digest::SHA256.new
@l = @sha256.digest("whatever")
@sha256.reset
cloned_l = @l.dup
subkey = @sha256.digest("whatever2")

puts @l
puts cloned_l

cloned_l.xor!(subkey)

puts @l
puts cloned_l

#outputs

�s��2�Y���%�\m �C�"�O
�s��2�Y���%�\m �C�"�O
XA'pP�
       �7B�,��vd��,]|R>�f
XA'pP�
       �7B�,��vd��,]|R>�f

I am absolutely lost on why it is not working correctly here because it
seems to be the same exact pattern to me?! Can anyone see what is going
wrong?

···

--
Posted via http://www.ruby-forum.com/.

okay I think it is either a bug in .dup or more likely a bug in .xor!,
because if it works or not depends on the string size:

@l = "aaaaaaaaaaaaaaaaaaaa"
cloned_l = @l.dup
subkey = "bbbbbbbbbbbbbbbbbbbb"
puts @l
puts cloned_l
cloned_l.xor!(subkey)
puts @l
puts cloned_l

#output

aaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaa
[formating doesn't show up]

···

###############################

require 'xor'
require 'openssl'

@l = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
cloned_l = @l.dup
subkey = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"

puts @l
puts cloned_l
cloned_l.xor!(subkey)
puts @l
puts cloned_l

#output
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
[formating doesn't show up]
[formating doesn't show up]

where do you think the bug is?

--
Posted via http://www.ruby-forum.com/.

If you run ruby with full warnings enabled (-w command line option),
you get warnings when a method is redefined - this might help here.

-- Matma Rex