newlines in array problem

C

Cyril Joe

Is there a way to get rid of newlines in an array?
Example: array = ["hel\nlo", "bl\nah"]

I want the output to be new_array = ["hello", "blah"]

I tried:

array.each do |el|
el.delete("\n")
end

But that didn't work. Can't figure this out. Any suggestions?
 
J

Jesús Gabriel y Galán

Is there a way to get rid of newlines in an array?
Example: array =3D ["hel\nlo", "bl\nah"]

I want the output to be new_array =3D ["hello", "blah"]

I tried:

array.each do |el|
=A0el.delete("\n")
end

But that didn't work. Can't figure this out. Any suggestions?

String#delete returns a copy of the string, it doesn't modify it in
place. Use String#delete!

ruby-1.8.7-p334 :017 > array =3D ["hel\nlo", "bl\nah"]
=3D> ["hel\nlo", "bl\nah"]
ruby-1.8.7-p334 :018 > array.each {|word| word.delete!("\n")}
=3D> ["hello", "blah"]
ruby-1.8.7-p334 :019 > array
=3D> ["hello", "blah"]


Jesus.
 
7

7stud --

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"]
 
J

Jesús Gabriel y Galán

each() doesn't create a new array. =A0Why not use map()? =A0And 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 :).
array =3D ["hel\nlo", "bl\nah"]

new_arr =3D array.map do |str|
=A0str.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?
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. =A0On 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|
=A0str.delete!("\n")
end

You don't need map! here, cause you don't want to change which object
each position references. You just want to modify the strings
themselves. What I would say is that, if you need to preserve the
original strings (because they are referenced by other variables) but
use the same array, do:

a =3D "hel\nlo"
b =3D "bl\nah"
array =3D [a,b]

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

The bang version of map, because you want to change the array, but the
non-bang version of delete so as to keep the original strings. The two
cases you propose above have less use cases, IMHO.

Jesus.
 
7

7stud --

t =

#991555:
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/.=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top