我很惭愧地问这个问题,因为它看起来应该很明显,但是如何判断字符串中的给定字符在 Ruby 中是大写还是小写呢?String
我在课堂上看不到明显的罐头解决方案。
我采用了以下方法,其中不考虑非 ASCII 代码:
def is_lower?(c)
c >= 'a' && c <= 'z'
end
def is_upper?(c)
! is_lower(c)
end
我考虑的其他事情是:
def is_lower?(c)
c != c.upcase
end
有什么更惯用的吗?