Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Titleize doesn't take all-uppercase words into account

#2944

When using the "my string".titleize method, words which are already all-uppercase are processed like any other word. This leads to certain strings being displayed differently to how one might intend.

For example:

"BLT sandwich".titleize => "Blt Sandwich"
"USA today".titleize => "Usa Today"

Below is the (incredibly hacky) version I use, which first checks whether a string is all-uppercase. If it is, the string is not capitalized.

def titleize(word)
  words = word.split(' ')
  processed_words = []
  words.each do |aWord|
    aWord = aWord.capitalize unless /^[A-Z]*$/.match(aWord)
    processed_words << aWord
  end
  processed_words.join(" ")
end

Reported by Owain Hunt · July 23rd, 2009 @ 05:46 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. John Pignata
    John Pignata

    I extended ActiveSupport to allow me to do this in a project where we had some uppercase acronyms that we wanted to maintain for various reasons. The approach is not very different from Owain's. If a string has two consecutive upcased letters, there's some funny business going on.

    -jp

    July 28th, 2009 @ 06:18 AM

  2. Michael Koziarski
    Michael Koziarski
    • Tag changed from 2.x, capitalize, inflections, string, titleize to 2.x, bugmash, capitalize, inflections, string, titleize

    August 3rd, 2009 @ 06:01 AM

  3. Matt Duncan
    Matt Duncan

    +1 to John's patch which checks for two consecutive uppercase letters.

    Helpful for the following:

    >> "OpenID".titleize
    => "OpenID"
    >> "ActiveRecord".titleize
    => "Active Record"
    

    August 8th, 2009 @ 01:23 AM

  4. Matt Duncan
    Matt Duncan

    Verified John's patch - sorry, should have included that in my previous update.

    August 8th, 2009 @ 01:28 AM

  5. Josh Sharpe
    Josh Sharpe

    verified and +1
    This has always bugged me!

    August 8th, 2009 @ 02:41 AM

  6. Elad Meidar
    Elad Meidar

    Patch does not apply. i wasn't able to determine the reason so the patch was not fixed.

    August 8th, 2009 @ 04:01 AM

  7. Elad Meidar
    Elad Meidar

    +1 verified Patch applies, and all tests run.
    Previous error was due to my mistake, sorry.

    August 8th, 2009 @ 04:26 AM

  8. Steve St. Martin
    Steve St. Martin

    -1 patch applies and may be a great solution for edge cases however poses a major issue as "MY ALL CAPITALIZED STRING".titleize will output all uppercase when users input data with caps-lock which is likely to happen often.

    perhaps a different solution would be to allow a config option of words to ignore in specific inflector methods?

    August 8th, 2009 @ 05:02 AM

  9. Josh Sharpe
    Josh Sharpe

    I agree with steve, but I don't like the config option idea... What if we changed the patch to detect a caps-locked entry:

    pseudo-code

    if str == str.upcase
    titleize_old_way else
    titleize_patch_way end

    That's a bit hacky, I know....but I think it'd do the trick

    August 8th, 2009 @ 05:52 AM

  10. Steve St. Martin
    Steve St. Martin

    although the config idea sounds off, it is more likely to be backwords compatible and to cleanup all kinds of crazy things "Oops i ACCidentally CaPPitalized some stuff i wasn't supposed to", as things are less likely to be an acronym.

    The inflector already has the ability for exceptions built in:

    ActiveSupport::Inflector.inflections do |inflect|
      inflect.human /OpenID/, 'OpenID'
    end
    

    problem lies in that humanize takes an underscored string, so at this point the acronym is already lost, so titelize can be modified to check inflections.humans itself, or possibly move underscore into humanize

    August 8th, 2009 @ 06:23 AM

  11. Josh Sharpe
    Josh Sharpe

    Here's an addition to the previous patch that titleizes ALL CAPS strings.

    August 8th, 2009 @ 06:24 AM

  12. Steve St. Martin
    Steve St. Martin

    turns out a custom inflection option will probably need to be made and patched in as the real problem is that .capitalize is always called last and will ultimately destroy any replacements we do, so those words would need to be replaced after capitalize gets to them, or skip capetilize if ex: inflections.acronyms.included?(word)

    August 8th, 2009 @ 07:11 AM

  13. John Pignata
    John Pignata

    Great comments. My vote would be to close this ticket, leave titleize alone, and encourage developers who have edge needs such as technical or medical content to write their own titleize helper to take into account everything you gents have discussed above. titleize as it stands does as advertised.

    -jp

    August 8th, 2009 @ 02:07 PM

  14. Rizwan Reza
    Rizwan Reza

    -1 I also vote to leave titleize helper alone. It works as expected.

    August 8th, 2009 @ 02:23 PM

  15. Matt Duncan
    Matt Duncan

    @stevestmartin great comments. I'd vote to leave titleize alone for now also. This is something which would be a lot easier to solve on an individual application basis for anyone who needs it without causing unexpected results.

    August 8th, 2009 @ 02:27 PM

  16. Elad Meidar
    Elad Meidar

    -1 leave #titlieze alone too, it is an edge cases that require this much attention anyways.

    August 8th, 2009 @ 04:21 PM

  17. Greg Sterndale
    Greg Sterndale
    1. I think edge cases should be handled in the Inflector.

    August 8th, 2009 @ 05:35 PM

  18. Greg Sterndale
  19. Milan Dobrota
  20. Milan Dobrota
  21. Dan Croak
    Dan Croak

    -1 ditto eize.sus and gsterndale

    August 8th, 2009 @ 07:40 PM

  22. chucknelson
    chucknelson

    -1 reading all the above comments, it definitely makes sense for edge cases to be handled on a case-by-case basis and not to disturb the core functionality of titleize.

    August 8th, 2009 @ 07:45 PM

  23. Rizwan Reza
  24. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to wontfix

    August 8th, 2009 @ 11:06 PM

  25. Jeremy Kemper
    Jeremy Kemper
    • Tag changed from 2.x, bugmash, capitalize, inflections, string, titleize to 2.x, capitalize, inflections, string, titleize

    August 8th, 2009 @ 11:26 PM