From: Robert Klemme Date: 2009-12-10T18:15:55+09:00 Subject: Re: which is better? 2009/12/9 Florian Gilcher : > > 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 `
' 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/