Strip method

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?

This works fine …

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

Hi –

On Fri, 2 May 2008, Amasa M. wrote:

output:
" b1"
“b2”

expected output:
“b1”
“b2”

What is wrong?

Your expectation :slight_smile: 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

On May 1, 9:39 pm, Amasa M. [email protected] wrote:

Posted viahttp://www.ruby-forum.com/.
"

a1\t b1
a2\tb2

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

2008/5/1 Amasa M. [email protected]:

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/

On Thu, May 1, 2008 at 10:00 PM, William J. [email protected]
wrote:

}

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

Or split on whitespace instead of on tabs alone.

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

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