Hacker News new | ask | show | jobs
by sergiotapia 3622 days ago
Ruby 2.4 adds a new #match? method for regular expressions which is three times faster than any Regexp method in Ruby 2.3:

Why does Ruby have 4 different regexp match functions?

Regexp#match?: 2630002.5 i/s Regexp#===: 872217.5 i/s - 3.02x slower Regexp#=~: 859713.0 i/s - 3.06x slower Regexp#match: 539361.3 i/s - 4.88x slower

1 comments

So there is choice of course.

match returns match data, and sets the $~ variable

=== returns true or false, setting the $~ variable

=~ returns integer (position) or nil, setting $~

The newest one match? returns a boolean, not setting $~

Also, === is the method used in case statement matching.