Ok. I’m a loser. I’ve been told and warned. But I still got bit.
If I do:
irb(main):001:0> GOLF = [ 1, 2, 3]
=> [1, 2, 3]
irb(main):002:0> g = GOLF
=> [1, 2, 3]
irb(main):003:0> g << 4
=> [1, 2, 3, 4]
irb(main):004:0> g
=> [1, 2, 3, 4]
irb(main):005:0> h = GOLF
=> [1, 2, 3, 4]
Notice that h is [ 1, 2, 3, 4 ] and GOLF has changed its value.
I understand why. Thats not my question.
My question is what or how do most ruby people do this?
Do you simply do this:
irb(main):001:0> GOLF = [ 1, 2, 3 ]
=> [1, 2, 3]
irb(main):002:0> g = GOLF.dup
=> [1, 2, 3]
irb(main):003:0> g << 4
=> [1, 2, 3, 4]
irb(main):004:0> h = GOLF
=> [1, 2, 3]
Or, do you usually hide GOLF inside a method – which does the dup. How
is this handled most of the time?
Thank you very much,
pedz