Ok. I'm a loser. I've been told and warned. But I still got bit.
Man, you're too hard on yourself!
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?
If you want GOLF to be immutable, you can freeze it:
$ irb --simple-prompt
GOLF = [ 1, 2, 3 ]
=> [1, 2, 3]
GOLF.freeze
=> [1, 2, 3]
h = GOLF
=> [1, 2, 3]
h << 4
TypeError: can't modify frozen array from (irb):4:in `<<' from (irb):4