Which is better?

Hi,

I want to delete the numbers in a string, for example,

x="abcd1234"

I have two ways:

x.gsub! /\d+/,""

x[/\d+/]=""

Which one is better?
I personally prefer the second one.

Thanks.

try also String#delete

···

On Wed, Dec 9, 2009 at 11:10 AM, Ruby Newbee <rubynewbee@gmail.com> wrote:

x.gsub! /\d+/,""

x[/\d+/]=""

The first. It doesn't fail if the string contains no numbers:

x ="abcd"

x[/\d+/]=""
IndexError: regexp not matched
         from (irb):10:in `='
         from (irb):10
         from /usr/local/bin/irb19:12:in `<main>'

Regards,
Florian

···

On Dec 8, 2009, at 11:40 PM, Ruby Newbee wrote:

Hi,

I want to delete the numbers in a string, for example,

x="abcd1234"

I have two ways:

x.gsub! /\d+/,""

x[/\d+/]=""

Which one is better?
I personally prefer the second one.

And then there's also

irb(main):001:0> "123ab".tr "0-9", ""
=> "ab"
irb(main):002:0> "123ab".tr! "0-9", ""
=> "ab"

Cheers

robert

···

2009/12/9 Florian Gilcher <flo@andersground.net>:

On Dec 8, 2009, at 11:40 PM, Ruby Newbee wrote:

Hi,

I want to delete the numbers in a string, for example,

x="abcd1234"

I have two ways:

x.gsub! /\d+/,""

x[/\d+/]=""

Which one is better?
I personally prefer the second one.

The first. It doesn't fail if the string contains no numbers:

x ="abcd"

x[/\d+/]=""
IndexError: regexp not matched
from (irb):10:in `='
from (irb):10
from /usr/local/bin/irb19:12:in `<main>'

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote:

And then there's also

irb(main):001:0> "123ab".tr "0-9", ""
=> "ab"
irb(main):002:0> "123ab".tr! "0-9", ""
=> "ab"

But why no parentheses?:
irb
>> $-w=true
=> true
>> "abcd1234".gsub! /\d+/,''
(irb):13: warning: ambiguous first argument; put parentheses or even spaces
=> "abcd"
>> "123ab".tr! "0-9",""
=> "ab"

what is the ambiguity in the first case, and why is there no warning in the second case?

···

--
Wybo

Seems to be related to the regexp:

10:56:19 $ ruby19 -wce 'o.gsub! /\d/, ""'
-e:1: warning: ambiguous first argument; put parentheses or even spaces
Syntax OK
10:56:42 $ ruby19 -wce 'o.gsub! "d", ""'
Syntax OK
10:56:50 $ ruby19 -wce 'o.gsub!(/\d/, "")'
Syntax OK
10:57:01 $

Why? No idea.

Cheers

robert

···

2009/12/10 Wybo Dekker <wybo@servalys.nl>:

Robert Klemme wrote:

And then there's also

irb(main):001:0> "123ab".tr "0-9", ""
=> "ab"
irb(main):002:0> "123ab".tr! "0-9", ""
=> "ab"

But why no parentheses?:
irb

$-w=true

=> true

"abcd1234".gsub! /\d+/,''

(irb):13: warning: ambiguous first argument; put parentheses or even spaces
=> "abcd"

"123ab".tr! "0-9",""

=> "ab"

what is the ambiguity in the first case, and why is there no warning in the
second case?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Hi,

···

Am Donnerstag, 10. Dez 2009, 18:58:04 +0900 schrieb Robert Klemme:

2009/12/10 Wybo Dekker <wybo@servalys.nl>:
>
> But why no parentheses?:
> irb
>>> $-w=true
> => true
>>> "abcd1234".gsub! /\d+/,''
> (irb):13: warning: ambiguous first argument; put parentheses or even spaces
> => "abcd"
>>> "123ab".tr! "0-9",""
> => "ab"
>
> what is the ambiguity in the first case, and why is there no warning in the
> second case?

Seems to be related to the regexp:

10:56:19 $ ruby19 -wce 'o.gsub! /\d/, ""'
-e:1: warning: ambiguous first argument; put parentheses or even spaces
Syntax OK
10:56:42 $ ruby19 -wce 'o.gsub! "d", ""'
Syntax OK
10:56:50 $ ruby19 -wce 'o.gsub!(/\d/, "")'
Syntax OK
10:57:01 $

Why? No idea.

The slash could also be a division operator. Say:

  $ ruby -wce 'o.gsub! %r/\d/, ""'
  $ ruby -wce 'o.gsub! %r{\d}, ""'

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
*
Discover String#notempty? at <http://raa.ruby-lang.org/project/step&gt;\.