RichardOnRails
2009-Jul-09 00:11 UTC
Curious why copying an array element by element fails when using strip!
Hi, I wanted to copy one array into another, element by element, so that I could edit the source data as necessary. I simultaneously changed a source element using "strip!". The latter step corrupted the copying. Without the bang operator, all works as expected. The above question, source code and output are stored at http://www.pastie.org/539430. This is not an earth-shattering problem. I''m just curious why it doesn''t work ... especially because it took me a good while to figure out the cause of my app''s failure. Thanks in Advance, Richard
Simon Macneall
2009-Jul-09 00:21 UTC
Re: Curious why copying an array element by element fails when using strip!
Hi, Did you read the documentation on strip and strip!? (http://ruby-doc.org/core/classes/String.html#M000820) strip! modifies the existing string (returning itself if a modification occurred and nil if nothing was done), while strip does the modification and returns a new string. Simon On Thu, 09 Jul 2009 08:11:30 +0800, RichardOnRails <RichardDummyMailbox58407-gP6xRNRnnqSxhq/XJNNIW0EOCMrvLtNR@public.gmane.org> wrote:> > Hi, > > I wanted to copy one array into another, element by element, so that I > could edit the source data as necessary. I simultaneously changed a > source element using "strip!". The latter step corrupted the > copying. Without the bang operator, all works as expected. > > The above question, source code and output are stored at > http://www.pastie.org/539430. > > This is not an earth-shattering problem. I''m just curious why it > doesn''t work ... especially because it took me a good while to figure > out the cause of my app''s failure. > > Thanks in Advance, > Richard >
Jamey Cribbs
2009-Jul-09 00:24 UTC
Re: Curious why copying an array element by element fails when using strip!
The strip! method isn''t corrupting the copy. It''s that the strip! method returns nil if nothing was stripped from the string. That''s why you are getting nil for the second array element in the copy. Jamey On Wed, Jul 8, 2009 at 8:11 PM, RichardOnRails<RichardDummyMailbox58407-gP6xRNRnnqSxhq/XJNNIW0EOCMrvLtNR@public.gmane.org> wrote:> > Hi, > > I wanted to copy one array into another, element by element, so that I > could edit the source data as necessary. I simultaneously changed a > source element using "strip!". The latter step corrupted the > copying. Without the bang operator, all works as expected. > > The above question, source code and output are stored at > http://www.pastie.org/539430. > > This is not an earth-shattering problem. I''m just curious why it > doesn''t work ... especially because it took me a good while to figure > out the cause of my app''s failure. > > Thanks in Advance, > Richard > > > >
RichardOnRails
2009-Jul-09 00:41 UTC
Re: Curious why copying an array element by element fails when using strip!
Hi Simon & Jamey, I forgot the first commandment of programming: RTFM. Sorry, guys. Thanks for you excellent responses. Best wishes, Richard On Jul 8, 8:11 pm, RichardOnRails <RichardDummyMailbox58...-FtJgd9dCuO3JTKoYRCtP1UEOCMrvLtNR@public.gmane.org> wrote:> Hi, > > I wanted to copy one array into another, element by element, so that I > could edit the source data as necessary. I simultaneously changed a > source element using "strip!". The latter step corrupted the > copying. Without the bang operator, all works as expected. > > The above question, source code and output are stored athttp://www.pastie.org/539430. > > This is not an earth-shattering problem. I''m just curious why it > doesn''t work ... especially because it took me a good while to figure > out the cause of my app''s failure. > > Thanks in Advance, > Richard