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 -- Newsgroups: comp.lang.ruby Subject: Re: newlines in array problem Date: Thu, 7 Apr 2011 18:29:05 -0500 Organization: Service de news de lacave.net Lines: 58 Message-ID: <320bd4f1961690b7d4f8335c8e1fed53@ruby-forum.com> References: <030a5c75c68358523471052f22f04f7b@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: Quoted-printable X-Trace: talisker.lacave.net 1302218970 10747 65.111.164.187 (7 Apr 2011 23:29:30 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Thu, 7 Apr 2011 23:29:30 +0000 (UTC) In-Reply-To: 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: 381151 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <320bd4f1961690b7d4f8335c8e1fed53@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2488 "Jes=C3=BAs Gabriel y Gal=C3=A1n" wrote in pos= t = #991555: > On Thu, Apr 7, 2011 at 8:34 PM, 7stud -- wrote= : >> 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. > > This obviously depends on what he needs. The fact that each doesn't > create a new array can be a good thing :-). > While I realize it isn't always definitive, let's re-read what the op = actually said: >>> Example: array =3D ["hel\nlo", "bl\nah"] >>> I want the output to be new_array =3D ["hello", "blah"] >> array =3D ["hel\nlo", "bl\nah"] >> >> new_arr =3D array.map do |str| >> str.delete!("\n") >> end > > This I don't understand. You are modifying the original strings but > creating a new array with them. What could be the use case for this? > Well, let's see what I said about that: > 2) You need to be careful using delete!() because > it will change the strings in the original array too: "careful" meaning, "Dear op, you don't want to do that". >> 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 =3D ["hel\nlo", "bl\nah"] >> >> array.map! do |str| >> str.delete!("\n") >> end > > You don't need map! here, Yeah, I edited that out before you posted. My final suggestion uses = map() and delete(). -- = Posted via http://www.ruby-forum.com/.=