regexp match and nil

Discussion in 'Ruby' started by Adam Akhtar, Aug 26, 2008.

  1. Adam Akhtar

    Adam Akhtar Guest

    heres my function

    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.
    --
    Posted via http://www.ruby-forum.com/.
    Adam Akhtar, Aug 26, 2008
    #1
    1. Advertising

  2. On Tue, Aug 26, 2008 at 9:29 AM, Adam Akhtar <> wrote:
    > heres my function
    >
    > 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.


    With your above code you are trying to match the regexp twice:
    one for the =~ and one for the match, which is not optimal. I would
    do it like this:

    def remove_for_such_and_such(a_string)
    m = a_string.match(%r{(.+)for.*}i)
    a_string = m[1].strip if m
    a_string
    end

    Anyway you need an if, because you need to know if the string matched or not.
    If you want to avoid using if at all costs, you can play with rescue,
    but I think
    it's not as clear as checking explicitly. YMMV.

    Jesus.
    Jesús Gabriel y Galán, Aug 26, 2008
    #2
    1. Advertising

  3. Adam Akhtar

    Adam Akhtar Guest

    thanks guys.. ill look into sub as well...couldnt tell what would happen
    there if it didnt match but looks interesting.

    --
    Posted via http://www.ruby-forum.com/.
    Adam Akhtar, Aug 26, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candler

    puts nil generates "nil\n"

    Brian Candler, Nov 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    103
  2. John Carter
    Replies:
    64
    Views:
    621
    Klaus Stein
    May 19, 2005
  3. Christoffer Sawicki
    Replies:
    5
    Views:
    240
    Christoffer Sawicki
    Sep 2, 2006
  4. Old Echo
    Replies:
    1
    Views:
    173
    Adam Shelly
    Sep 4, 2008
  5. Bigmac Turdsplash
    Replies:
    15
    Views:
    163
    Paul Smith
    Sep 8, 2009
Loading...

Share This Page