What is the equal-tilde operator in ruby mean?

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

The equal-tilde operator in ruby is the “match” operator.  It take an regular expression on the left hand side and the string to match on the right hand side.  The expression …

/or/ =~ “Hello World”

will return 7 because a match is found on index 7 of the string.  index starts at 0.

The expression

/abc/ =~ “Hello World”

will return nil because there is no match.