Ruby: What's the proper syntax for a boolean regex method?

前端 未结 10 2423
野趣味
野趣味 2021-02-18 18:40

What is the proper syntax for a method that checks a string for a pattern, and returns true or false if the regex matches?

Basic idea:

def has_regex?(str         


        
10条回答
  •  佛祖请我去吃肉
    2021-02-18 19:12

    To make it return true/false switch the position of pattern and string and use case equality ===

    def has_regex?(string)
        pattern = /something/i
        return pattern === string
    end
    

    I absolutely needed it to return true boolean value and digged around. It is actually documented in regexp class http://www.ruby-doc.org/core-2.1.3/Regexp.html#method-i-3D-3D-3D

提交回复
热议问题