← Back to Upcase

Titleize and roman numerals, dashes, apostrophes, etc


(Justin Gordon) #1

I’m simply trying to convert uppercased company names into proper names.

Company names can include:

  1. Dashes
  2. Apostrophes
  3. Roman Numerals
  4. Text like LLC, LP, INC which should stay uppercase.

I thought I might be able to use acronyms like this:

ACRONYMS = %W( LP III IV VI VII VIII IX GI)
ActiveSupport::Inflector.inflections(:en) do |inflect|
  ACRONYMS.each { |a| inflect.acronym(a) }
end

However, the conversion does not take into account word breaks, so having VI and VII does not work. For example, the conversion of “ADVISORS”.titleize is “Ad VI Sors”, as the VI becomes a whole word.

Dashes get removed.

It seems like there should be a generic gem for this generic problem, but I didn’t find one. Is this problem really not that common? What’s the best solution besides completely hacking the current inflection library?


(Derek Prior) #2

Yeah, I can’t imagine this is a very common problem. For starters, titleizing a company name is not guaranteed to be in keeping with the prefered name for the company (for instance IBM, Coca-Cola Company, or thoughtbot).If this is still something you want to do, here’s the simplest thing I could think of that could possibly work:

"ADVISORS-LLC VI".titleize.gsub(/\b(llc|VI)\b/i) { |match| match.upcase } # => "Advisors LLC VI"

You’d have to add exceptions for every thing you wanted to remain capitialized.