12

I'm looking to check if a string is all capitals in Rails. How would I go about doing that?

I'm writing my own custom pluralize helper method and I would something be passing words like "WORD" and sometimes "Word" - I want to test if my word is all caps so I can return "WORDS" - with a capital "S" in the end if the word is plural (vs. "WORDs").

Thanks!

KCE
  • 1,159
  • 8
  • 25
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175
  • possible duplicate of [How can I check a word is already all uppercase in Ruby?](http://stackoverflow.com/questions/8529595/how-can-i-check-a-word-is-already-all-uppercase-in-ruby) – philant May 29 '15 at 09:15
  • Ah no, this question was asked prior to that one, so if anything that question should be closed as a dup of this one. – Taryn East Jun 01 '15 at 07:03
  • @philant - you should not vote-to-close as duplicate form both sides... just from one side. – Taryn East Jun 01 '15 at 07:03
  • 1
    @TarynEast yeah I know, but I couldn't undo it. Thanks. – philant Jun 01 '15 at 08:57

3 Answers3

26

Do this:

str == str.upcase

E.g:

str = "DOG"
str == str.upcase  # true
str = "cat"
str == str.upcase  # false

Hence the code for your scenario will be:

# In the code below `upcase` is required after `str.pluralize` to transform 
# DOGs to DOGS
str = str.pluralize.upcase if str == str.upcase
Harish Shetty
  • 64,083
  • 21
  • 152
  • 198
  • 1
    It is so beautifully idiomatic! Have in mind though that for very long strings it will incur time and memory penalties because it requires allocating yet another string and traversing original one twice. Regular-expression-based solutions are more optimal. – skalee Nov 19 '14 at 21:33
3

Thanks to regular expressions, it is very simple. Just use [[:upper:]] character class which allows all upper case letters, including but not limited to those present in ASCII-8.

Depending on what do you need exactly:

# allows upper case characters only
/\A[[:upper:]]*\Z/   =~ "YOURSTRING"

# additionally disallows empty strings
/\A[[:upper:]]+\Z/   =~ "YOURSTRING"

# also allows white spaces (multi-word strings)
/\A[[:upper:]\s]*\Z/ =~ "YOUR STRING"

# allows everything but lower case letters
/\A[^[:lower:]]*\Z/  =~ "YOUR 123_STRING!"

Ruby doc: http://www.ruby-doc.org/core-2.1.4/Regexp.html

skalee
  • 12,331
  • 6
  • 55
  • 57
2

Or this:

str =~ /^[A-Z]+$/

e.g.:

"DOG" =~ /^[A-Z]+$/    # 0
"cat" =~ /^[A-Z]+$/    # nil 
James A. Rosen
  • 64,193
  • 61
  • 179
  • 261
  • 1
    Does not work when string has anything else than A-Z in it. DÖG or CÄT or DOG123 for example. – Priit Mar 01 '10 at 07:17
  • 1
    Quite true. If internationalization is an issue, the `str == str.upcase` might work better, but that depends on the definition of `upcase`. On my system, `"coöperation".upcase` is `"COöPERATION"`, which doesn't really help. Ruby 1.9 is more encoding-aware; for those on 1.8, there's the Unicode gem: http://rubygems.org/gems/unicode – James A. Rosen Mar 01 '10 at 13:07
  • Improper anchors, apart from aforementioned issues it will match strings like `"lower\nUPPER"` – skalee Nov 19 '14 at 21:26