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

前端 未结 10 2424
野趣味
野趣味 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条回答
  •  猫巷女王i
    2021-02-18 19:14

    If you want the put the pattern in a method, you can just do

    def has_my_pattern(st)
        st =~ /pattern/
    end
    

    Or, perhaps better, put the pattern in a class variable instead?

提交回复
热议问题