tags:

views:

144

answers:

6

I saw this on a screencast and couldn't figure out what it was. Reference sheets just pile it in with other operators as a general pattern match operator.

+1  A: 

Well, the reference is correct, it is the "matches this regex" operator.

if var =~ /myregex/ then something end
Tesserex
+2  A: 

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.

ealdent
Thanks for this!
CCSab
A: 

I believe this is a pattern matching operator used with regex.

Andrew Hubbs
A: 

Regular expression string matching. Here's a detailed list of operators: http://phrogz.net/programmingruby/tut_expressions.html#table_7.1

Curtis
A: 

From ruby-doc :

str =~ obj => fixnum or nil

Match—If obj is a Regexp, use it as a pattern to match against str,and returns the position the match starts, or nil if there is no match. Otherwise, invokes obj.=~, passing str as an argument. The default =~ in Object returns false.

"cat o' 9 tails" =~ /\d/   #=> 7
"cat o' 9 tails" =~ 9      #=> false
j.
A: 

Regular expression string matching:

puts true if url =~ /google.com/

You can read '=~' as 'is matching'.

Erik