# Function appends same value to string based on number of function calls

**URL:** <https://www.ruby-forum.com/t/function-appends-same-value-to-string-based-on-number-of-function-calls/135768>\
**Category:** Ruby\
**Created:** [April 24, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/function-appends-same-value-to-string-based-on-number-of-function-calls/135768 "2008-04-24T20:00:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/function-appends-same-value-to-string-based-on-number-of-function-calls/135768/1 "2008-04-24T20:00:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/function-appends-same-value-to-string-based-on-number-of-function-calls/135768/2 "2008-04-24T20:12:05Z")

</div>

On Apr 24, 2008, at 2:00 PM, removed\_email\_address@domain.invalid 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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2008, 8:49pm UTC](https://www.ruby-forum.com/t/function-appends-same-value-to-string-based-on-number-of-function-calls/135768/3 "2008-04-24T20:49:13Z")

</div>

-----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-----

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/function-appends-same-value-to-string-based-on-number-of-function-calls/135768/4 "2008-04-24T20:40:58Z")

</div>

On Apr 24, 11:11 am, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2008, 8:53pm UTC](https://www.ruby-forum.com/t/function-appends-same-value-to-string-based-on-number-of-function-calls/135768/5 "2008-04-24T20:53:55Z")

</div>

On Apr 24, 11:35 am, removed\_email\_address@domain.invalid wrote:

> > > of call to Testappend::get\_some\_value, the string “-New Value” is
> 
> > Rob B. [http://agileconsultingllc.com](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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/function-appends-same-value-to-string-based-on-number-of-function-calls/135768/6 "2008-04-24T20:50:23Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

removed\_email\_address@domain.invalid 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](http://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](http://enigmail.mozdev.org)

iEYEARECAAYFAkgQ1fUACgkQbtAgaoJTgL9oswCfepMHKn77Bnp7Jf6hNDa4oZaN  
15YAn1m7aEB/kw7XS4e0AH5Ea73pYdA8  
=EO/v  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/function-appends-same-value-to-string-based-on-number-of-function-calls/135768/7 "2008-04-24T21:07:32Z")

</div>

On Apr 24, 11:47 am, Florian G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > but modifies the receiver in place.  
> > end
> 
> iEYEARECAAYFAkgQ1dcACgkQJA/zY0IIRZa6kQCeLJ8P6Dz1Gj9WBUWM73n0LGcU  
> 0McAnjzeEwBTNtYBkJatMDKMORNcdO/f  
> =WEi6  
> -----END PGP SIGNATURE-----

Thank you for the explanation.

Philippe

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2008, 9:08pm UTC](https://www.ruby-forum.com/t/function-appends-same-value-to-string-based-on-number-of-function-calls/135768/8 "2008-04-24T21:08:44Z")

</div>

On Apr 24, 11:47 am, Florian G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > but modifies the receiver in place.  
> > end
> 
> iEYEARECAAYFAkgQ1dcACgkQJA/zY0IIRZa6kQCeLJ8P6Dz1Gj9WBUWM73n0LGcU  
> 0McAnjzeEwBTNtYBkJatMDKMORNcdO/f  
> =WEi6  
> -----END PGP SIGNATURE-----

Thank you for the explanation.

Philippe

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2008, 9:05pm UTC](https://www.ruby-forum.com/t/function-appends-same-value-to-string-based-on-number-of-function-calls/135768/9 "2008-04-24T21:05:46Z")

</div>

- [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (19:56) schrieb:

> ```
> 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
