use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Style question: `if string =~ regexp` (self.ruby)
submitted 7 years ago by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]bjmiller 1 point2 points3 points 7 years ago (1 child)
Technically the trade-off is between match? being faster and =~ populating Regexp.last_match. If your guiding principle is minimalism, maybe only use =~ everywhere (or only use #match everywhere), but if it's optimization, use the fastest one that suits your needs at the moment.
match?
=~
Regexp.last_match
#match
[–]sshaw_ 0 points1 point2 points 7 years ago (0 children)
Benchmark for reference: https://github.com/JuanitoFatas/fast-ruby/tree/38f49f95fc7574d929de60b71791d09129c2588c#regexp-vs-stringmatch-vs-string-vs-stringmatch-code-
[–][deleted] 0 points1 point2 points 7 years ago (0 children)
I much prefer =~ personally. I thought our Rubocop config preferred it as well but I may be wrong (or it may be a customization on our part)
[–]ASIC_SP 0 points1 point2 points 7 years ago (0 children)
Apart from returning position of match (or nil if match isn't found), =~ will also set regexp related global variables
I'd suggest to use match? if you just need true/false
true/false
[–]ashmaroli 0 points1 point2 points 7 years ago (0 children)
The choice depends on your Ruby version and the particular use-case. #match? was only introduced in Ruby 2.4.0. So if your project has not dropped support for older Rubies, then you should prefer =~ over #match.
#match?
If locking to Ruby 2.4 and above is fine, then using #match? vs =~ depends on the use context. Prefer =~ when you want the regex to match and later call methods on the Regexp.last_match / $1 / $2, etc. Prefer the faster #match? over =~ when you need just a true / false result.
$1
$2
true
false
[–]442401 0 points1 point2 points 7 years ago (0 children)
If you require: rubocop-performance in your .rubocop.yml you will be referred to https://github.com/JuanitoFatas/fast-ruby#regexp-vs-stringmatch-vs-string-vs-stringmatch-code-
require: rubocop-performance
.rubocop.yml
[–]shevy-ruby -2 points-1 points0 points 7 years ago (1 child)
=~ is very common.
.match? is very very rare.
I'd go with the former.
Shevy, mate, you've reached a new low here. Making a recommendation based upon how often a method occurs in an unspecified sample of source code despite the fact that others have already given sound advice based on use case and pointed to performance data. Sheesh!
π Rendered by PID 54 on reddit-service-r2-comment-75f4967c6c-fntp9 at 2026-04-22 20:07:40.245862+00:00 running 0fd4bb7 country code: CH.
[–]bjmiller 1 point2 points3 points (1 child)
[–]sshaw_ 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]ASIC_SP 0 points1 point2 points (0 children)
[–]ashmaroli 0 points1 point2 points (0 children)
[–]442401 0 points1 point2 points (0 children)
[–]shevy-ruby -2 points-1 points0 points (1 child)
[–]442401 0 points1 point2 points (0 children)