2

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

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189

3 Answers3

5

You can try titleize, but also add gsub

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

It returns:

irb(main):006:0> "hello-world".titleize.gsub(' ', '-')
"Hello-World"
Stanislav Mekhonoshin
  • 4,276
  • 2
  • 20
  • 25
2

There isn't an inflector that will do what you want, but you can do this, which is quick and dirty:

'hello-world'.split('-').map(&:capitalize).join('-')
ipd
  • 5,674
  • 3
  • 34
  • 49
1

Try this i hope this will helping you

"hello-world".split('-').map(&:capitalize)*'-'
djadam
  • 649
  • 1
  • 4
  • 20