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?

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?

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

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.

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

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
[binary string formatting doesn’t display here]

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?

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

cloned_l = @l.dup
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 how it
changes is based on previous versions.

However, fast_xor only supports in place operations and sometimes I need
to
get @l.xor! without the in place because it makes it so my @l value
isn’t what it should be the next time I actually do need to change it.

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. I don’t want to take the massive speed
hit of using a pure ruby implementation of xor because fast_xor is 1000
times faster and this is for a cryptographic process so I don’t want a
ruby xor speed bottle neck when all the rest of the intensive stuff is
being done in C (a language I do not know, and am not really good at
writing wrappers at all, so I would prefer to use fast_xor even if it
requires some fairly ugly code to get the behavior I need)

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]

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

@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]

I think the bug must actually be in .xor! because if I replace that step
with .upcase! it still works as expected. So I guess my issue this
entire time has been a buggy gem. Hopefully I can find another fast way
to xor strings together…