From: Florian Gilcher Date: 2008-04-25T03:47:56+09:00 Subject: Re: Function appends same value to string based on number of function calls -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 > > class TestAppend > attr_accessor :text > > def initialize(attributes = {}) > @text = "Start of string" > end > > def get_some_value > value = @text > value << '-New Value' > return value > end > > end value = @text does not copy the string and << doesn't return a string but modifies the receiver in place. What you are doing is getting a reference to @text which just has another name. Then you modify @text by using << on value (which is the same object as @text). If you do not wish to modify @text, use "+" or string interpolation. ===code=== def get_some_value @text + "-New Value" #dies if @text == nil end #or def get_some_value "#{@text}-New Value" #doesn't die, but looks ugly if @text == nil end ===code=== Regards, Florian Gilcher -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) iEYEARECAAYFAkgQ1dcACgkQJA/zY0IIRZa6kQCeLJ8P6Dz1Gj9WBUWM73n0LGcU 0McAnjzeEwBTNtYBkJatMDKMORNcdO/f =WEi6 -----END PGP SIGNATURE-----