On Sat, Oct 31, 2009 at 5:50 PM, Dave Anderson <anderson / crocker.com> wrote:


> One practical thing I can draw out of this is the difference between
> these two functions:
>
>  ¨Âåæ Øß±¨ óô>  ¨ÂôòÛ°§Ø>  ¨Âîä
>
>  ¨Âåæ Øß²¨ óô> str = 'X' + str[ 1..-1 ]
>  ¨Âîä
>
> The X_1 function alters the passed-in object in place, while the X_2
> function modifies a local copy. I don't recall reading this in the
> literature anywhere.

Actually, the X_2 function doesn't make a local copy of the string
passed as the argument.

It ends up creating three new strings.

1 A copy of the literal string 'X'
2 A string which is the result of str[1..-1]
3 A string which is the concatenation of strings 1 and 2 which is then
bound to the local variable str. and will be the result of the method
call since it's the last expression and the value of an assignment
expression is the right hand side of the assignment.


-- 
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale