Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #6893
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: issue with stripping tabs |
| Date | 2013-11-23 13:22 +0100 |
| Message-ID | <bfbl0nFlq4cU1@mid.individual.net> (permalink) |
| References | <5d694d88-a4cb-4129-a4c5-b9de5f9dd4bb@googlegroups.com> |
On 23.11.2013 10:07, helmut_blass@web.de wrote:
> without success I try to strip leading and trailing tabs:
> I tried:
> str.gsub(/\s+/, "")
> str.gsub(/\t/, "")
> str.strip.gsub(/\t/, "")
>
> none of them works.
>
> any suggestions?
Where's your problem? They all work - in a way
irb(main):004:0> strings=["a", "\ta", "a\t", "\ta\tb\t"]
=> ["a", "\ta", "a\t", "\ta\tb\t"]
irb(main):005:0> strings.map {|str| str.gsub(/\s+/, "")}
=> ["a", "a", "a", "ab"]
irb(main):006:0> strings.map {|str| str.gsub(/\t/, "")}
=> ["a", "a", "a", "ab"]
irb(main):007:0> strings.map {|str| str.strip.gsub(/\t/, "")}
=> ["a", "a", "a", "ab"]
You might want to do
irb(main):008:0> strings.map {|str| str.sub(/\A\t+/, "").sub(/\t+\z/, "")}
=> ["a", "a", "a", "a\tb"]
But #strip is much simpler, even though it removes other whitespace as well:
irb(main):009:0> strings.map {|str| str.strip}
=> ["a", "a", "a", "a\tb"]
Cheers
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Back to comp.lang.ruby | Previous | Next — Previous in thread | Find similar
issue with stripping tabs helmut_blass@web.de - 2013-11-23 01:07 -0800 Re: issue with stripping tabs Robert Klemme <shortcutter@googlemail.com> - 2013-11-23 13:22 +0100
csiph-web