In that case, /([^@]+)@(.+$)/.match("[email protected]")[0] returns the whole string matched by the expression, and [1] and [2] return the first and second groups. That's why the throwaway is there. To get rid of the throwaway, this is a possibility:
username, domain = */([^@]+)@(.+$)/.match("[email protected]").captures
Of course, your string better match, otherwise you'll get a NoMethodError.
Re:What are your Ruby Regex Idioms?
by steveklabnik on Monday November 30, 2009 @11:20AM
(#968060)
$1 and $2 are probably still there do to Ruby's Perl heritage. I don't particularly mind their use, but that's probably due to my long history with Perl.
FWIW, I personally prefer the #match method. Some of the examples in this article just make me want to hurt people. Especially usage of $1, $2, etc.