[RUBY] ActiveSupport underscore is not the inverse of camelize

When you use a word that consists of multiple words in an identifier, you cannot include spaces, so you must somehow represent a word break. For example, the following notation is used.

ActiveSupport provides a set of useful methods for converting between these.

You can use String # underscore to convert CamelCase to snake_case. You can use String # camelize to convert snake_case to UpperCamelCase [^ lcc].

[^ lcc]: Use camelize (: lower) to convert to lowerCamelCase.

As below:

require "active_support/inflector"

p "BookTitle".underscore # => "book_title"
p "book_title".camelize  # => "BookTitle"

Looking at this, ʻunderscore and camelize` are inverse transformations of each other.

require "active_support/inflector"

p "BookTitle".underscore.camelize  # => "BookTitle"
p "book_title".camelize.underscore # => "book_title"

It seems (always) to return to the original when both are overlapped like.

However, in some cases this is not the case. that is

require "active_support/inflector"

p "OfficialURL".underscore # => "official_url"
p "CSVFile".underscore     # => "csv_file"

This is a case that contains words that consist only of uppercase letters, such as ʻURLandCSV. The ʻunderscore method splits CSVFile into CSV and File into a snake_case, as humans would expect. However, this loses the information that the converted ʻurlandcsv were originally spelled in uppercase only, so camelize` them will not restore them.

require "active_support/inflector"

p "OfficialURL".underscore.camelize # => "OfficialUrl"
p "CSVFile".underscore.camelize     # => "CsvFile"

It was an example that a convenient method could fall into a pitfall if the specifications are not well understood and used. (Someday, I was addicted to [^ ot], actually!)

[^ ot]: Initially, it was made into a snake_case with a code like gsub (/. (? = [AZ]) /) {$ & +" _ "} .downcase, and later the processing using ActiveSupport was mixed. Sometimes something went wrong.

Recommended Posts

ActiveSupport underscore is not the inverse of camelize
The idea of cutting off when the error is not resolved
[Rails] When the layout change of devise is not reflected
'% 02d' What is the percentage of% 2?
What is testing? ・ About the importance of testing
What is the data structure of ActionText?
The list of installed apps is not displayed in getPackageManager (Android11 ​​/ API level 30)
Is it mainstream not to write the closing tag of <P> tag in Javadoc?
[Java] Calculate the day of the week from the date (Calendar class is not used)
What is JSP? ~ Let's know the basics of JSP !! ~
The order of Java method modifiers is fixed
The devise error message is not displayed properly.
GoogleMap is not displayed until the browser is reloaded
The public key for jenkins-2.249.1-1.1.noarch.rpm is not installed
The official name of Spring MVC is Spring Web MVC
When the project is not displayed in eclipse
Ebean.update () is not executed in the inherited model.
The essence of AspectJ's mood-why your `@Transactional` is ignored
When the hover of Eclipse is hard to see
In Time.strptime,% j (total date of the year) is
Is drainTo of LinkedBlockingQueue safe? I followed the source
The comparison of enums is ==, and equals is good [Java]
Do not easily round the result of decimal calculation
[JUnit5] Dealing with "the reference of assertEquals is ambiguous"
The question of which is better, if or switch
When the month of the date is acquired, the January shift
[Swift] This is the solution! Illustration of Delegate implementation
PATH does not pass ... The cause is the character code
[Error] The app is not displayed in the production environment
How is the next value of the Time object correct?
When rewriting the CMD of docker image of Pod in the manifest of k8s, command is not good
Even if I write the setting of STRICT_QUOTE_ESCAPING in CATALINA_OPTS in tomcat8.5, it is not reflected.
[Eclipse] Version 1.8.0_261 of the JVM is not suitable for this product. Version: 11 or greater is required Error