removing something from the end of each item in an array

S

Simon Schuster

in this case a "\n"...

["blah\n", "la\n", "hooray\n"]

array.collect { |x| x - "\n" } doesn't work, and I can't manage to
find any methods which might do the trick.. pretty basic, but I'm
still a beginner! thanks.
 
C

Chris Carter

in this case a "\n"...

["blah\n", "la\n", "hooray\n"]

array.collect { |x| x - "\n" } doesn't work, and I can't manage to
find any methods which might do the trick.. pretty basic, but I'm
still a beginner! thanks.

array.map {|x| x.chomp } will strip whitespace including new-lines.
 
J

Jano Svitok

in this case a "\n"...

["blah\n", "la\n", "hooray\n"]

array.collect { |x| x - "\n" } doesn't work, and I can't manage to
find any methods which might do the trick.. pretty basic, but I'm
still a beginner! thanks.

array.map {|x| x.chomp } will strip whitespace including new-lines.

If you by chance want to do that in place, use map! instead of map.
 
S

Simon Schuster

ahh! interesting. I tried array.chomp and got a private method error.
thanks, I'll look into this map business.
 
X

Xavier Noria

in this case a "\n"...

["blah\n", "la\n", "hooray\n"]

array.collect { |x| x - "\n" } doesn't work, and I can't manage to
find any methods which might do the trick.. pretty basic, but I'm
still a beginner! thanks.

array.map {|x| x.chomp } will strip whitespace including new-lines.

That sounds to me as if it meant

%r{\s+\z}

Just in case, if $/ has not been changed chomp removes any trailing
single occurrence of \n, \r, \r\n:

irb(main):005:0> "foo \r\n".chomp
=> "foo "
irb(main):006:0> "foo \n\n".chomp
=> "foo \n"

-- fxn
 
W

William James

in this case a "\n"...

["blah\n", "la\n", "hooray\n"]

array.collect { |x| x - "\n" } doesn't work, and I can't manage to
find any methods which might do the trick.. pretty basic, but I'm
still a beginner! thanks.

If you want to remove all leading and trailing whitespace:

array.map{|x| x.strip }
 
B

Bertram Scharpf

Hi,

Am Samstag, 18. Aug 2007, 03:35:11 +0900 schrieb Chris Carter:
["blah\n", "la\n", "hooray\n"]

array.map {|x| x.chomp } will strip whitespace including new-lines.

Here, this just strips the newlines. Depending on what Simon
meant there's also

array.map! {|x| x.chop }
array.map! {|x| x.rstrip }
array.map! {|x| x.strip }

I often even say

array.each { |x| x.strip! }

but that belongs into the poison chest and is for people who
know what they're doing.

Bertram
 
K

Kaldrenon

in this case a "\n"...

["blah\n", "la\n", "hooray\n"]

array.collect { |x| x - "\n" } doesn't work, and I can't manage to
find any methods which might do the trick.. pretty basic, but I'm
still a beginner! thanks.

As others have already said, probably the best way to remove \n from a
string's end is chomp or chomp! (the version with ! does an edit in
place, the version without returns the modification without changing
the original string)

However, this method won't work if, as your subject line implies, you
end up wanting to remove something else from a string.

I would recommend looking at http://www.whytheluckystiff.net/ruby/pickaxe/html/ref_c_string.html

It will show you everything you need to know about lovely methods like
delete, gsub, tr, and more.

HTH
-Andrew
 
D

David A. Black

Hi --

in this case a "\n"...

["blah\n", "la\n", "hooray\n"]

array.collect { |x| x - "\n" } doesn't work, and I can't manage to
find any methods which might do the trick.. pretty basic, but I'm
still a beginner! thanks.

array.map {|x| x.chomp } will strip whitespace including new-lines.

If you by chance want to do that in place, use map! instead of map.

Or each and chomp! which will prevent you from creating extra string
objects.


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top