I am new to Ruby and on this site.
The following two functions are different: one changes the variable outside the function, and the other does not.
def m1 (x)
x << "4"
end
def m2 (x)
x = x + "4"
end
str="123"
m2(str) #str remains unchanged 123
m1(str) #str is changed to 1234
I would like to make sure I understood correctly -
When m1 is called, the str reference is copied and passed to the function that sees it as x. The operator <changes x that refer to the original str, so this str is modified by this operation.
When m2 is called, the str reference is copied and passed to the function that sees it as x. The + operator creates a new line, and assigning x = x + "4" simply redirects x to a new line, leaving the original str variable intact.
Right?
thank

yonso source
share