Title Case for Ruby Strings

How to convert a string to Title Case

  • Break into words
  • Capitalise each word
  • Join back together
split(/(\W)/).map(&:capitalize).join

or example

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

or

s = "hello world"Title Case for Ruby Strings
s.split(/(\W)/).map(&:capitalize).join

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

Your email address will not be published. Required fields are marked *