# Capitalize only when 4 or more letters are caps

**URL:** <https://www.ruby-forum.com/t/capitalize-only-when-4-or-more-letters-are-caps/141929>\
**Category:** Ruby\
**Created:** [July 1, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/capitalize-only-when-4-or-more-letters-are-caps/141929 "2008-07-01T21:19:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [July 1, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/capitalize-only-when-4-or-more-letters-are-caps/141929/1 "2008-07-01T21:19:36Z")

</div>

I have a chunk of text that could be sentences. If someone types in all  
caps, I would like to use capitalize on just the words that are in all  
caps. If I just use it on the entire object, my sentences get messed up  
and don’t start with a capital letter like they should.

I was wondering what an easy solution might be. I figured I could break  
the string apart at the spaces to get the words easily enough, but what  
about the conditional to only use capitalize on words with 4 or more  
capital letters. That part has me stumped. I think I need a regular  
expression for it, but I’m not sure what it should be.

Surely I am not the first person to want to do this.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [July 1, 2008, 9:58pm UTC](https://www.ruby-forum.com/t/capitalize-only-when-4-or-more-letters-are-caps/141929/2 "2008-07-01T21:58:46Z")

</div>

It’s not really a complete solution, but you can work off of this:

string = “A BB CCC DDDD EEEE FFFFF”

result = string.split.inject([]) do |a,x|  
x.downcase! if x[/[A-Z]{4,}/]  
a \<\< x  
end  
p result.join(" ") #=\> “A BB CCC dddd eeee fffff”
