which is better?

R

Ruby Newbee

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.
 
F

Florian Gilcher

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>'

Regards,
Florian
 
R

Robert Klemme

2009/12/9 Florian Gilcher said:
Hi,

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

x=3D"abcd1234"

I have two ways:

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

x[/\d+/]=3D""


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


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

x =3D"abcd"

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

And then there's also

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

Cheers

robert

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

Wybo Dekker

Robert said:
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(irb):13: warning: ambiguous first argument; put parentheses or even spaces
=> "abcd"=> "ab"

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

Robert Klemme

2009/12/10 Wybo Dekker said:
But why no parentheses?:
irb
(irb):13: warning: ambiguous first argument; put parentheses or even spaces
=> "abcd"
=> "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.

Cheers

robert
 
B

Bertram Scharpf

Hi,

Am Donnerstag, 10. Dez 2009, 18:58:04 +0900 schrieb Robert Klemme:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top