Removing something from the end of each item in an array

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.

On 8/17/07, Simon S. [email protected] wrote:

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.

On 8/17/07, Chris C. [email protected] wrote:

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.

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

On Aug 17, 1:30 pm, “Simon S.” [email protected] wrote:

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 }

On Aug 17, 2007, at 8:35 PM, Chris C. wrote:

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

On Aug 17, 2:30 pm, “Simon S.” [email protected] wrote:

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

Hi –

On Sat, 18 Aug 2007, Jano S. wrote:

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

Hi,

Am Samstag, 18. Aug 2007, 03:35:11 +0900 schrieb Chris C.:

On 8/17/07, Simon S. [email protected] wrote:

[“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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs