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

前端 未结 10 2373
野趣味
野趣味 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:31

    I do not have enough reputation to comment, so I will answer instead.

    Use of ===, as suggested by viljar, is advised against in the Ruby Style Guide and will cause Rubocop complaints (under default rules).

    I find the most readable way to be:

    def match?(string)
      !(string =~ /something/i).nil?
    end
    

提交回复
热议问题