Hi,
array = [“one”, “two”, “three”]
array_copy = array
array_copy << “four”
p array
p array_copy
#=>
[“one”, “two”, “three”, “four”]
[“one”, “two”, “three”, “four”]
How can I add an element to “array_copy” and leave “array” unaltered?
Cheers.
Hi,
array = [“one”, “two”, “three”]
array_copy = array
array_copy << “four”
p array
p array_copy
#=>
[“one”, “two”, “three”, “four”]
[“one”, “two”, “three”, “four”]
How can I add an element to “array_copy” and leave “array” unaltered?
Cheers.
Answered my own question.
Array.dup ist the way to go.
array = [“one”, “two”, “three”]
array_copy = array.clone
array_copy << “four”
p array
p array_copy
output:
[“one”, “two”, “three”]
[“one”, “two”, “three”, “four”]
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs