What is the “=~” operator in Ruby?

前端 未结 7 679
情深已故
情深已故 2020-12-01 09:56

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.

7条回答
  •  无人及你
    2020-12-01 10:58

    As the other answers already stated, =~ is the regular expression vs string match operator.

    Note: The =~ operator is not commutative

    Please consider the note below from the ruby doc site, as I have seen yet only the first form

    str =~ regexp 
    

    used in the other answers:

    Note: str =~ regexp is not the same as regexp =~ str. Strings captured from named capture groups are assigned to local variables only in the second case.

    Here is the documentation for the second form: link

提交回复
热议问题