ruby-on-railsstringcapitalizationhyphenationcap

Capitalize both parts of a hyphenated word with Rails


I'm trying to capitalize both parts of a hyphenated word with Rails:

"hello-world".capitalize
# => Hello-world

"hello-world".titleize
# => Hello World

Is there a quick way of doing this? If not then I will write a custom solution, I can figure that out, but I'm hoping there is some really quick and dirty method that can do this for me


Solution

  • You can try titleize, but also add gsub

    "hello-world".titleize.gsub(' ', '-')
    

    It returns:

    irb(main):006:0> "hello-world".titleize.gsub(' ', '-')
    "Hello-World"