[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

removing something from the end of each item in an array

Simon Schuster

8/17/2007 6:30:00 PM

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.

8 Answers

Chris Carter

8/17/2007 6:35:00 PM

0

On 8/17/07, Simon Schuster <significants@gmail.com> 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.


--
Chris Carter
concentrationstudios.com
brynmawrcs.com

Jano Svitok

8/17/2007 6:39:00 PM

0

On 8/17/07, Chris Carter <cdcarter@gmail.com> wrote:
> On 8/17/07, Simon Schuster <significants@gmail.com> 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.

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

Simon Schuster

8/17/2007 6:54:00 PM

0

ahh! interesting. I tried array.chomp and got a private method error.
thanks, I'll look into this map business.
>
> array.map {|x| x.chomp } will strip whitespace including new-lines.
>
>
> --
> Chris Carter
> concentrationstudios.com
> brynmawrcs.com
>
>

Xavier Noria

8/17/2007 7:05:00 PM

0

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

> On 8/17/07, Simon Schuster <significants@gmail.com> 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.

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


William James

8/17/2007 7:12:00 PM

0

On Aug 17, 1:30 pm, "Simon Schuster" <significa...@gmail.com> 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 }

Bertram Scharpf

8/17/2007 7:30:00 PM

0

Hi,

Am Samstag, 18. Aug 2007, 03:35:11 +0900 schrieb Chris Carter:
> On 8/17/07, Simon Schuster <significants@gmail.com> 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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Kaldrenon

8/17/2007 7:43:00 PM

0

On Aug 17, 2:30 pm, "Simon Schuster" <significa...@gmail.com> 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_s...

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

HTH
-Andrew


David A. Black

8/17/2007 7:51:00 PM

0