Ruby 中的 if 语句使用正则表达式 [英] If statement in Ruby using Regex

查看:26
本文介绍了Ruby 中的 if 语句使用正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了注释行外,一切似乎都正常:

Everything seems to be working fine except the commented line:

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

第四次检查.

s[0]/AZ/ 比较的正确 if 语句是什么?

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

推荐答案

试试这个...

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

这篇关于Ruby 中的 if 语句使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆