SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: =~?

  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    =~?

    I saw

    word =~/^qu/

    in a program, what does it do:?
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    might as well show the whole method

    def first_vowel(word)
    if word =~/^qu/
    2
    else
    word.gsub(/[aeiou].*$/, '').size
    end
    end

    Any ideas as I'm lost om what its even doling?
    "Oh, and Jenkins--apparently your mother died this morning."

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,060
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    ~= is the ruby match operator.
    It matches a string to a regular expression.

    e.g.

    Code Ruby:
    'Pullo' =~ /o/ 
    # => 4

    If there is no match, it will return nil.

    As for the method you posted:

    Code Ruby:
    def first_vowel(word)
      if word =~/^qu/
        2
      else
        word.gsub(/[aeiou].*$/, '').size
      end
    end

    it checks to see if the word it receives as an argument starts with a "qu".
    If so, it returns 2

    Otherwise it replaces everything after (and including) the occurrence of the first vowel, then returns the size of what's left.

  4. #4
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks
    "Oh, and Jenkins--apparently your mother died this morning."

  5. #5
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,343
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    This is the best and only answer.
    Closing the thread.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •