Can anyone explain the behavior
Scenario-1
str = "hello"
str1 = str
puts str #=> hello
puts str1 #=> hello
str1 = "hi"
puts str1 #=> hi
puts str #=> hello
Here, changing the value of str1
has no effect on the value of str
.
Scenario-2
str = "hello"
str1 = str
str1.gsub! "hello", "whoa!"
puts str1 #=> whoa
puts str #=> whoa
Shoudn't the gsub!
effect only the str1
? Why is it changing str
? If str1
just holds the reference to str
, then why did the value not change in Scenario-1?