From: "Peña, Botp" Date: 2008-08-26T17:03:22+09:00 Subject: Re: regexp match and nil From: Adam Akhtar [mailto:adamtemporary@gmail.com] # def remove_for_such_and_such(a_string) # if (%r{(.+)for.*}i =~ a_string) # match = a_string.match(%r{(.+)for.*}i) # a_string = match[1].strip # end # a_string # end # i want to know if that could be written better. I wrote the if clause # because sometimes a string may not match the regular expression. If i # remove the "if" clause it might lead to match returning nil which will # stop my program. Im wondring is there a way to avoid using # if?? if so it could help performance especially if this function was # used with huge arrays of strings. try string#sub 0> class String 1> def remove_for_such_and_such 2> sub(%r{(.+)for.*}i){$1.strip} 2> end 1> end => nil 0> x="fooforbar" => "fooforbar" 0> x.remove_for_such_and_such => "foo" kind regards -botp