我想检查 Rails 中的字符串是否全部为大写字母。 我该怎么做?
我正在编写自己的自定义复数化辅助方法,我会传递诸如“WORD”之类的词,有时是“Word”——我想测试我的词是否全部大写,这样我就可以返回“WORDS”——用如果单词是复数形式(相对于“WORDs”),则末尾大写“S”。
谢谢!
最佳答案
这样做:
str == str.upcase
例如:
str = "DOG"
str == str.upcase # true
str = "cat"
str == str.upcase # false
因此您的场景代码将是:
# In the code below `upcase` is required after `str.pluralize` to transform
# DOGs to DOGS
str = str.pluralize.upcase if str == str.upcase
关于ruby-on-rails - 检查字符串是否在 Rails 中全部大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2350213/