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.
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, 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.
--
Chris Carter
concentrationstudios.com
brynmawrcs.com
If you want to remove all leading and trailing whitespace:
array.map{|x| x.strip }
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.
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
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.
If you by chance want to do that in place, use map! instead of map.
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.
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
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, 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.
Hi,
> ["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
Am Samstag, 18. Aug 2007, 03:35:11 +0900 schrieb Chris Carter:
On 8/17/07, Simon Schuster <significants@gmail.com> wrote:
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
Hi --
On Sat, 18 Aug 2007, Jano Svitok wrote:
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.
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)