What Ruby or Rails DSL will format a string "mccdougal"
to "McDougal"
, and at the same time leave the string "McDougal"
as is?
Passing titleize
to "McDougal"
results in the following:
"McDougal".titleize # => "Mc Dougal"
There isn't a Rails helper to my knowledge that can handle this case. It's a non-standard edge case which needs special handling. You could create a custom string inflection, however. You could drop this bit of code in an initializer:
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.human /mcdougal/, 'McDougal'
end
And then when you call "mcdougal".humanize
, you'll get "McDougal"
You aren't going to find something that correctly formats a name like that. The reason is because the reason that the M and D in McDougal are capitalized is an arbitrary regional thing. The only way that I can think of doing something like that is with a lookup table. I would say that the best you'll get programatically is "mcdougal".capitalize
=> "Mcdougal". I would argue that you can't and shouldn't guess regional capitalizations.
If However you are making an app for the Irish however, and it absolutely needs to be done. I would make a lookup table to solve the problem. It's tedious, but you'll find a finite amount of cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With