-1

Everything seems to be working fine except the commented line:

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

and the fourth check.

What is the correct if statement for s[0] and /AZ/ comparison?

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
4

4 に答える 4

1

これを試して...

def starts_with_consonant? s
  /^[^aeiou\d\W]/i =~ s ? true : false
end
于 2016-06-14T05:01:57.580 に答える
0

また、あなたの正規表現が何を達成しようとしているのかわからないので、修正を提案することはできません. しかし、メソッド全体については、=== 演算子を使用し、オプションで正規表現の大文字と小文字を区別しないようにすることで、単純に保ちiます。

def starts_with_consonant?(s)
    /^[bcdfghjklmnpqrstvwxyz]/i === s
end
于 2013-10-07T01:26:48.003 に答える
0

正規表現を使えば簡単です:

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

これにより、文字列の最初の文字が子音のセットと一致します。!!出力を true/false に強制します。

于 2013-10-06T22:11:06.277 に答える
-1

これも効く

    def starts_with_consonant? s
      return /^[^aeiou]/i === s
    end
于 2015-11-01T12:46:20.430 に答える