From: Julian Tarkhanov Date: 2007-01-28T18:38:14+09:00 Subject: Re: pass by reference? On Jan 27, 2007, at 12:27 AM, Andy Koch wrote: > Is there a way to pass variables by reference into function. > > I have large string to pass and the pass by value seems to be > eating up too much memory. Strings are mutable objects and should be treated as such :-) def append_sour_word(strbuf) strbuf << " ...uck!" end text = "Good l" append_sour_word(text) #=> "Good luck" is returned AND is contained in text now text #=> "Good luck" def assign_concatenated(strbuf) strbuf = strbuf + " ...uck!" end assign_concatenated(text) #=> "This is some new text" is returned but you lost the reference inside the function scope so the actual _value_ stays the same text #=> still "Good luck", because you assigned another reference to your variable def replace_content(strbuf) strbuf.replace(strbuf + " ...uck!") end replace_content(text) "Good l..uck!..uck!", but now with a desired side effect text #=> "Good l..uck!..uck!", now you replaced the object with another one Basically, to avoid confusion - if you eating up RAM avoid object _duplication_, because that's what eats it up. But it's perfectly possible to massage objects in place without having to ask them for their "actual value". -- Julian 'Julik' Tarkhanov please send all personal mail to me at julik.nl