Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to extract groups from a regex match in Ruby without globals or temporary variables. Code snippets supporting
if "" =~ /@(.*)/
raise "bad email"
# => ""
match = /@(.*)/.match("")
if match
raise "bad email"
# => ""
$1 if "" =~ /@(.*)/ or raise "bad email" # => ""
require 'andand'
/@(.*)/.match("").andand[1] # => ""
""[/@(.*)/, 1] or raise "bad email"
# => ""

This comment has been minimized.

Copy link

joshuapinter commented Dec 8, 2018

I found that match = /@(.*)/.match("") gave different results than "" =~ /@(.*)/.

Same Regex and same body but result was different so ended up using =~.

I take it back. I might not have been using .captures after using .match(). It works great, actually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.