From: vannuys@... Date: 2008-04-25T03:50:14+09:00 Subject: Re: Function appends same value to string based on number of function calls On Apr 24, 11:35 am, vann...@gmail.com wrote: > On Apr 24, 11:11 am, Rob Biedenharn > wrote: > > > > > On Apr 24, 2008, at 2:00 PM, vann...@gmail.com 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 Biedenharn http://agileconsultingllc.com > > R...@AgileConsultingLLC.com > > > > 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 > > 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 Sorry, this works, the second line is << instead of = def get_some_value value = '' value << @text value << '-New Value' return value end Philippe