.dub metod, which duplicates an object

By 11/10/2019 Posts
 
def 
 change_string(str)
 str.replace("New string content!") 
end
 
s = "Original string content!"
change_string(s.dup)

puts s 

Or you can use .freeze method

s = "Original string content!"
s.freeze

change_string(s)

Note that there’s no corresponding unfreeze method. Freezing is forever.

Also, you can use clone. It’s like as dup , but if you clone a frozen object, the clone is also frozen—whereas if you dup a frozen object, the duplicate isn’t frozen.


codeByPasha

Author codeByPasha

More posts by codeByPasha

Leave a Reply