How can I check a word is already all uppercase?

a = "Go234"
a.match(/\p{Lower}/) # => #<MatchData "o">

b = "GO234"
b.match(/\p{Lower}/) # => nil

c = "123"
c.match(/\p{Lower}/) # => nil

d = "µ"
d.match(/\p{Lower}/) # => #<MatchData "µ">

So when the match result is nil, it is in uppercase already, else something is in lowercase.

Thank you @mu is too short mentioned that we should use /\p{Lower}/ instead to match non-English lower case letters.


You can compare the string with the same string but in uppercase:

'go234' == 'go234'.upcase  #=> false
'GO234' == 'GO234'.upcase  #=> true

Tags:

Ruby