What is the “=~” operator in Ruby?

It matches string to a regular expression.

'hello' =~ /^h/ # => 0

If there is no match, it will return nil. If you pass it invalid arguments (ie, left or right-hand sides are not correct), it will either throw a TypeError or return false.

Leave a Comment