Hi Guys,
Is this default ruby behaviour or is this a bug? Based on the number
of call to Testappend::get_some_value, the string “-New Value” is
appended x number of times. If you first assign value = ‘’ instead of
value = @text, the behaviour is different and the “-New Value” string
is appended only once.
version of ruby: ruby 1.8.6 (2007-03-13 patchlevel 0) [x86_64-linux]
Thanks.
Philippe
module ATest
def ATest.main
t = TestAppend.new
puts t.get_some_value ==> Start of string-New Value
puts t.get_some_value ==> Start of string-New Value-New Value
puts t.get_some_value ==> Start of string-New Value-New Value-New
Value
end
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
end
ATest::main
On Apr 24, 2008, at 2:00 PM, [email protected] wrote:
Hi Guys,
Is this default ruby behaviour or is this a bug? Based on the number
of call to Testappend::get_some_value, the string “-New Value” is
appended x number of times. If you first assign value = ‘’ instead of
value = @text, the behaviour is different and the “-New Value” string
is appended only once.
Do you mean:
def get_some_value
value = ‘’
value << ‘-New Value’
return value
end
If so, where do you do anything with the String object to which @text
refers?
BTW, you could simplify your original to just:
def get_some_value
@text << ‘-New Value’
end
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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 G.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)
iEYEARECAAYFAkgQ1dcACgkQJA/zY0IIRZa6kQCeLJ8P6Dz1Gj9WBUWM73n0LGcU
0McAnjzeEwBTNtYBkJatMDKMORNcdO/f
=WEi6
-----END PGP SIGNATURE-----
On Apr 24, 11:11 am, Rob B. [email protected]
wrote:
Do you mean:
def get_some_value
Thanks.
puts t.get_some_value ==> Start of string-New Value
@text = “Start of string”
end
ATest::main
Rob,
I do not want to assign a new value to @text.
The “get_some_value” function should return a new string (value) which
is a concatenation of strings. In this case a simple concatenation of
@text and “-New Value”.
#This version will work and does not append “-New Value” x times based
on the number of function calls.
def get_some_value
value = ‘’
value = @text
value << ‘-New Value’
return value
end
Thank you for your help,
Philippe
On Apr 24, 11:35 am, [email protected] wrote:
of call to Testappend::get_some_value, the string “-New Value” is
Rob B. http://agileconsultingllc.com
module ATest
return value
I do not want to assign a new value to @text.
return value
end
Thank you for your help,
Philippe
Sorry, this works, the second line is << instead of =
def get_some_value
value = ‘’
value << @text
value << ‘-New Value’
return value
end
Philippe
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[email protected] wrote:
| Rob,
|
| I do not want to assign a new value to @text.
| The “get_some_value” function should return a new string (value) which
| is a concatenation of strings. In this case a simple concatenation of
| @text and “-New Value”.
def return_some_value
~ “#{@text} -New Value”
end
new_string = MyClass.return_some_value
Phillip G.
Twitter: twitter.com/cynicalryan
~ - You know you’ve been hacking too long when…
…your digital alarm clock goes off and you think “Bloody Macs!”
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAkgQ1fUACgkQbtAgaoJTgL9oswCfepMHKn77Bnp7Jf6hNDa4oZaN
15YAn1m7aEB/kw7XS4e0AH5Ea73pYdA8
=EO/v
-----END PGP SIGNATURE-----
On Apr 24, 11:47 am, Florian G. [email protected] wrote:
end
but modifies the receiver in place.
end
iEYEARECAAYFAkgQ1dcACgkQJA/zY0IIRZa6kQCeLJ8P6Dz1Gj9WBUWM73n0LGcU
0McAnjzeEwBTNtYBkJatMDKMORNcdO/f
=WEi6
-----END PGP SIGNATURE-----
Thank you for the explanation.
Philippe
On Apr 24, 11:47 am, Florian G. [email protected] wrote:
end
but modifies the receiver in place.
end
iEYEARECAAYFAkgQ1dcACgkQJA/zY0IIRZa6kQCeLJ8P6Dz1Gj9WBUWM73n0LGcU
0McAnjzeEwBTNtYBkJatMDKMORNcdO/f
=WEi6
-----END PGP SIGNATURE-----
Thank you for the explanation.
Philippe
def get_some_value
value = @text
value << '-New Value'
return value
end
You are appending to the string object pointed to by “value”, which is
the same object “@text” is pointing to.
You might want:
def get_some_value
“#{@text}-New Value”
end
mfg, simon … hth