If an expression in Ruby using regex

Everything seems to work just fine, except for the commented line:

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

and the fourth check.

What is the correct operator ifto compare s[0]and /AZ/?

def starts_with_consonant?(s)
   return false if s.length == 0
   #return false if not s[0].upcase =~ /AZ/
   n = "AEIOU"
   m = s[0]
   return true if not n.include? m.upcase
   false
 end

 puts starts_with_consonant?("Artyom") # false 1
 puts starts_with_consonant?("rtyom")  # true 2
 puts starts_with_consonant?("artyom") # false 3
 puts starts_with_consonant?("$rtyom") # false 4
 puts starts_with_consonant?("") # false 5
-1
source share
4 answers

try it...

def starts_with_consonant? s
  /^[^aeiou\d\W]/i =~ s ? true : false
end
+1
source

Easy with regex:

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

This corresponds to the first character of a line with a set of consonants. !!Forces the value true / false.

0
source

, , . , === operator i :

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

    def starts_with_consonant? s
      return /^[^aeiou]/i === s
    end
-1

All Articles