Title Case for Ruby Strings

How to convert a string to Title Case

  • Break into words
  • Capitalise each word
  • Join back together

or example

"hello world".split(/(\W)/).map(&:capitalize).join     "Hello World"


s = "hello world"Title Case for Ruby Strings

Or from a associated table

  • For a given entity p (call it a product)
  • Collect all of the sizes assigned to it
  • Get their string value and Title Case the result
  • Join all the results with a comma
p.sizes.collect{ |x| x.to_s.split(/(\W)/).map(&:capitalize).join }.join(','),

Leave a comment