author avatar

ayushsrivastava

Wed Oct 04 2023

in ruby if we have a variable called a = “HELLO” and then we assign it to a new variable b = a it does not create a deep copy of the string "Hello" stored in a. Instead, it creates a new variable b that references the same string object in memory as a. Both a and b will point to the same memory location, which means they will hold the same value and any changes made through one variable will be reflected in the other.

so if we do

b.upcase!

it will return

puts a  # Output: "HELLO"
puts b  # Output: "HELLO"

If we want to create a separate copy of the string, we can use the dup method or string manipulation methods to create a new string object with the same content.