Hello,
Looking for you suggestions.
- array = %w(a b c d e)
- tempArray = array
- array.clear
- puts tempArray.length
line 4 outputs: 0
Why does my tempArray get cleared also?
Any suggestions on how to keep the tempArray content from clearing out?
Thanks
Hello,
Looking for you suggestions.
line 4 outputs: 0
Why does my tempArray get cleared also?
Any suggestions on how to keep the tempArray content from clearing out?
Thanks
Parv G. wrote:
Hello,
Looking for you suggestions.
- array = %w(a b c d e)
- tempArray = array
- array.clear
- puts tempArray.length
line 4 outputs: 0
Why does my tempArray get cleared also?
Any suggestions on how to keep the tempArray content from clearing out?Thanks
because array and tempArray are references to the same Array instance.
array.object_id #=> -605829518
tempArray.object_id #=> -605829518
tempArray = array.dup #=> make a shallow copy.
array.clear
puts tempArray.length #=> 5
tempArray = array.dup #=> make a shallow copy.
array.clear
puts tempArray.length #=> 5
That should do the trick for me.
Thanks a lot!
Parv
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