0
@hac = HacCfgGroup.find(1897)
b = @hac
>> @hac.status
=> 2


>> @hac.status = 4
=> 4
>> b.status
=> 4

HacCfgGroup is an object with many attributes. How to let b keep its original value? spent so long on it still cannot figure out how to keep an object without changed.

2 Answers2

0

b and @hac are pointing at the same object. Try b = @hac.dup creating a duplicate object.

Ray Baxter
  • 3,181
  • 23
  • 27
  • seems not work?? I can still modify the b via @hac even though they are dup – liangwang chi Jan 22 '16 at 03:46
  • See the difference between `clone` and `dup` here: http://stackoverflow.com/questions/10183370/whats-the-difference-between-rubys-dup-and-clone-methods. – Ray Baxter Jan 22 '16 at 05:07
0

http://lukaszwrobel.pl/blog/copy-object-in-ruby

there are some explainations. more precisely clone is the answer