vannuys
4/24/2008 7:04:00 PM
On Apr 24, 11:47 am, Florian Gilcher <f...@andersground.net> wrote:
> -----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-----
Thank you for the explanation.
Philippe