How to format strings like "mccdougal" in "McDougal",

That Ruby or Rails DSL will format the string "mccdougal" to "McDougal" and at the same time leave the string "McDougal" as it is?

Passing titleize to "McDougal" results in the following:

 "McDougal".titleize # => "Mc Dougal" 
+5
source share
2 answers

There is no Rails helper at my disposal that can handle this case. This is a non-standard edge case, which requires special processing. However, you can create a custom line break. You can remove this bit of code in the initializer:

 ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.human /mcdougal/, 'McDougal' end 

And then when you call "mcdougal".humanize , you get "McDougal"

+5
source

You will not find something that correctly formats such a name. The reason is that the reason M and D at McDougal are capitalized is an arbitrary regional thing. The only way I can think of doing something like this is with a lookup table. I would say that the best you get programmatically is "mcdougal".capitalize =>" Mcdougal ". I would say that you cannot and should not guess the regional capital letters.

If, however, you are making an application for the Irish, and this is absolutely necessary. I would do a lookup table to solve this problem. This is tiring, but you will find a finite number of cases.

+4
source

Source: https://habr.com/ru/post/1244150/


All Articles