Today I encountered an algorithm to solve and the amazing Ruby syntax made me solve this problem with a succinct code with testing included.
All words in the title should be capitalised except a few ones like “a”, “the”, “to”, “at”, “in”, “with”, “and”, “but” and “or”.
To make this very short the solution is here:
require 'test/unit' class MyTest &lt; Test::Unit::TestCase test &quot;capitalization&quot; do assert_equal title_case("i love solving problems and it is fun!"), ("I Love Solving Problems and It Is Fun!") assert_equal title_case("wHy DoeS A biRd Fly?"), ("Why Does a Bird Fly?") end end def title_case(title) lower_list = %w(a the to at in with and but or) words = title.downcase.split words.each do |word| word.capitalize! unless lower_list.include?(word) end title = words.join(' ') end