Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

strip method

18 views
Skip to first unread message

Amasa Masiarek

unread,
May 1, 2008, 10:39:26 PM5/1/08
to
s = <<EOS
a1\t b1
a2\tb2
EOS

s.each{|e|
a = e.strip.split("\t")
p a[1]
}

output:
" b1"
"b2"

expected output:
"b1"
"b2"

What is wrong?
--
Posted via http://www.ruby-forum.com/.

Amasa Masiarek

unread,
May 1, 2008, 10:42:43 PM5/1/08
to
This works fine ...

s.each{|e|
a = e.gsub(' ','').split("\t")
p a[1]
}

Rick DeNatale

unread,
May 1, 2008, 10:48:29 PM5/1/08
to
2008/5/1 Amasa Masiarek <ad...@masiarek.com>:

> s = <<EOS
> a1\t b1
> a2\tb2
> EOS
>
> s.each{|e|
> a = e.strip.split("\t")
> p a[1]
> }
>
> output:
> " b1"
> "b2"
>
> expected output:
> "b1"
> "b2"
>
> What is wrong?

k$ qri String#strip
----------------------------------------------------------- String#strip
str.strip => new_str
------------------------------------------------------------------------
Returns a copy of str with leading and trailing whitespace removed.

Leading and trailing doesn't mean internal. So:

"a1\t b1".strip #=> "a1\t b1"

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

David A. Black

unread,
May 1, 2008, 10:48:38 PM5/1/08
to
Hi --

On Fri, 2 May 2008, Amasa Masiarek wrote:

> s = <<EOS
> a1\t b1
> a2\tb2
> EOS
>
> s.each{|e|
> a = e.strip.split("\t")
> p a[1]
> }
>
> output:
> " b1"
> "b2"
>
> expected output:
> "b1"
> "b2"
>
> What is wrong?

Your expectation :-) Sorry, I couldn't resist.

strip removes space on the right and left. If you start with
"a1\t b1\n", the stripped version is "a1\t b1". If you split that on
\t, you get "a1" and " b1". At no point have you removed the middle
space.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!

William James

unread,
May 1, 2008, 10:55:57 PM5/1/08
to


"


".strip.each{|x|
a = x.split("\t").map{|s| s.strip }
p a.last
}

Gordon Thiesfeld

unread,
May 2, 2008, 3:23:18 PM5/2/08
to

Or split on whitespace instead of on tabs alone.

s.each{|e|
a = e.split("\s")
p a[1]
}

0 new messages