"Jesús Gabriel y Galán" <jgabrielygalan / gmail.com> wrote in post 991555: > On Thu, Apr 7, 2011 at 8:34 PM, 7stud -- <bbxx789_05ss / yahoo.com> 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 ctually said: >>> Example: array = ["hel\nlo", "bl\nah"] >>> I want the output to be new_array = ["hello", "blah"] >> array = ["hel\nlo", "bl\nah"] >> >> new_arr = 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 = ["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 ap() and delete().