I have an input for users to enter a Twitter account in any different way and I want to extract the user account.For example:twitters = [    www.twitter.com/twitteruser1,    @twitteruser2,    twitteruser3,    https://twitter.com/twitteruser4,    https://www.twitter.com/twitteruser5,    www.twitter.com/twitteruser6,    http://www.twitter.com/twitteruser7,    http://www.twitter.com/twitteruser8,    twitter.com/twitteruser9]The script that I've written to extract the data is the following:twitters.each do |twitter|    # for the url    twitter_user = twitter.match(/twitter.com\/([^\/.]*)$/)    if twitter_user != nil        puts twitter_user[1]        next    end    # for @ beginning    twitter_user = twitter.match(/^@([^\/.]*)$/)    if twitter_user != nil        puts twitter_user[1]        next    end    # if we arrive, we haven't found any coincidence    puts twitterendIt actually works, outputting the following:twitteruser1twitteruser2twitteruser3twitteruser4twitteruser5twitteruser6twitteruser7twitteruser8twitteruser9But as I'm really newbie in Ruby I wanted to check for possible improvements.
Getting the Twitter account from various input formats
beginner;ruby;parsing;url;twitter
I would put the regular expressions into a list:TWITTER_PATTERNS = [  /twitter.com\/([^\/.]*)$/,    # www.twitter.com/twitteruser1  /^@([^\/.]*)$/,               # @twitteruser2]This function can iterate over that list:def twitter_user(twitter)  TWITTER_PATTERNS.each do |pattern|    return $1 if twitter =~ pattern  end  twitterendThis clearly separates the policy (what patterns are used to extract twitter users) from the mechanism (the loop we use to apply the policy).  A change to the function's implementation won't require a change to the patterns, and vice versa.