[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

vannuys

4/24/2008 5:56:00 PM

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

Rob Biedenharn

4/24/2008 6:11:00 PM

0

On Apr 24, 2008, at 2:00 PM, vannuys@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://agileconsult...
Rob@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
>




vannuys

4/24/2008 6:35:00 PM

0

On Apr 24, 11:11 am, Rob Biedenharn <R...@AgileConsultingLLC.com>
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://agileconsult...
> 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

Simon Krahnke

4/24/2008 6:36:00 PM

0

* <vannuys@gmail.com> (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

vannuys

4/24/2008 6:45:00 PM

0

On Apr 24, 11:35 am, vann...@gmail.com wrote:
> On Apr 24, 11:11 am, Rob Biedenharn <R...@AgileConsultingLLC.com>
> 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://agileconsult...
> > 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

Florian Gilcher

4/24/2008 6:48:00 PM

0

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

Phillip Gawlowski

4/24/2008 6:49:00 PM

0

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

vannuys@gmail.com 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 Gawlowski
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....

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

vannuys

4/24/2008 7:04:00 PM

0

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

vannuys

4/24/2008 7:05:00 PM

0

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