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.

Inflector.underscore() is not consistent with camelize()

#1355

From the documentation of ActiveSupport::Inflector

underscore()

The reverse of camelize. Makes an underscored, lowercase form from the expression in the string.

However, underscore doesn't handle consecutive caps characters correctly.


"ERDiagram".underscore  # "er_diagram"
"er_diagram".camelize   # "ErDiagram"
"ERDiagram".underscore.camelize == "ERDiagram"  # false

Reported by Nick Eskelinen · November 11th, 2008 @ 05:02 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Pratik
    Pratik
    • State changed from new to incomplete

    Can we please have a patch with failing tests ? http://guides.rails.info/contrib... should be helpful.

    Thanks !

    March 12th, 2009 @ 03:58 PM

  2. Nick Eskelinen
    Nick Eskelinen
    • Tag set to activeresource, bug, inflector

    Hmm, so I tried to write a failing test, but it appears this behavior is explicitly tested for (with incorrect behavior, IMO) in inflector_test_cases.rb:

    
      CamelToUnderscoreWithoutReverse = {
        "HTMLTidy"              => "html_tidy",
        "HTMLTidyGenerator"     => "html_tidy_generator",
        "FreeBSD"               => "free_bsd",
        "HTML"                  => "html",
      }
    

    So it looks like there are 2 options: 1) Fix documentation to not claim that camelize() and underscore() are inverse functions (and warn that string.underscore.camelize != string) 2) Fix underscore() to break on (all) caps boundaries and provide a facility for making exceptions (probably via the Inflector#inflections method)

    My personal feeling is that #1 Migrations will not run on a fresh database is ignoring the fundamental problem with the current behavior of underscore() -- there is no way to go from an underscored transformation back to the original camelized form.

    Comments?

    March 13th, 2009 @ 07:10 PM

  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  4. rails
    rails
    • State changed from incomplete to open
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 26th, 2011 @ 12:00 AM

  5. rails
    rails
    • State changed from open to stale

    February 26th, 2011 @ 12:00 AM