Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2481

Re: newlines in array problem

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!news2.euro.net!feeds.phibee-telecom.net!talisker.lacave.net!lacave.net!not-for-mail
From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: newlines in array problem
Date Thu, 7 Apr 2011 13:34:27 -0500
Organization Service de news de lacave.net
Lines 36
Message-ID <d9d8bc4bd3d3c1f79205895b94e3c974@ruby-forum.com> (permalink)
References <030a5c75c68358523471052f22f04f7b@ruby-forum.com>
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace talisker.lacave.net 1302201492 79184 65.111.164.187 (7 Apr 2011 18:38:12 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Thu, 7 Apr 2011 18:38:12 +0000 (UTC)
In-Reply-To <030a5c75c68358523471052f22f04f7b@ruby-forum.com>
X-Received-From This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway
X-Mail-Count 381143
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <d9d8bc4bd3d3c1f79205895b94e3c974@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:2481

Show key headers only | View raw


each() doesn't create a new array.  Why not use map()?  And you need to 
be careful using delete!() because it will change the strings in the 
original array too.

array = ["hel\nlo", "bl\nah"]

new_arr = array.map do |str|
  str.delete!("\n")
end

p new_arr
p array

--output:--
["hello", "blah"]
["hello", "blah"]


If you really want to preserve the original array, don't use delete! on 
the strings.  On the other hand, if you don't need two versions of the 
array hanging around in memory, then use all ! methods:

array = ["hel\nlo", "bl\nah"]

array.map! do |str|
  str.delete!("\n")
end

p array

--output:--
["hello", "blah"]

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

newlines in array problem Cyril Joe <cyril_jose@ymail.com> - 2011-04-07 11:38 -0500
  Re: newlines in array problem Michel Demazure <michel@demazure.com> - 2011-04-07 11:43 -0500
  Re: newlines in array problem Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-07 11:50 -0500
  Re: newlines in array problem Cyril Jose <cyril_jose@ymail.com> - 2011-04-07 13:08 -0500
  Re: newlines in array problem 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-07 13:34 -0500
    Re: newlines in array problem Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-07 14:17 -0500
      Re: newlines in array problem 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-07 18:29 -0500

csiph-web