Posted 10 months ago. Visible to the public.

Ruby: Return boolean for regex comparison

A collection of code snippets which return a boolean value for a regex comparison.

Copy
regexp.match?(string) # Recommended for Ruby >= 2.4 !!(string =~ regexp) # Recommended for older Rubies regexp === string !(regexp !~ string)

The Ruby 2.4 method Regexp#match? does not set globals like $~ or $1, so it should be more performant.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Author of this card:

Avatar
Emanuel De
Last edit:
10 months ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Emanuel De to makandropedia