If statement in Ruby using Regex

前端 未结 4 1218
终归单人心
终归单人心 2021-01-29 07:30

Everything seems to be working fine except the commented line:

#return false if not s[0].upcase =~ /AZ/

and the fourth check.

What is t

相关标签:
4条回答
  • 2021-01-29 07:49

    I'm also not sure what your regex is trying to achieve, so I can't suggest a fix. But for the entire method, I'd keep it simple by using the === operator and making the regex case-insensitive with the i option:

    def starts_with_consonant?(s)
        /^[bcdfghjklmnpqrstvwxyz]/i === s
    end
    
    0 讨论(0)
  • 2021-01-29 07:55

    It's easy with regex:

    def starts_with_consonant?(s)
       !!(s =~  /^[bcdfghjklmnpqrstvwxyz]/i)
    end
    

    This matches the first character of the string with the set of consonants. The !! forces the output to true/false.

    0 讨论(0)
  • 2021-01-29 07:59

    This works too

        def starts_with_consonant? s
          return /^[^aeiou]/i === s
        end
    
    0 讨论(0)
  • 2021-01-29 08:07

    try this...

    def starts_with_consonant? s
      /^[^aeiou\d\W]/i =~ s ? true : false
    end
    
    0 讨论(0)
提交回复
热议问题