Skip to content

Instantly share code, notes, and snippets.

@lidi
Created August 29, 2013 12:44
Show Gist options
  • Save lidi/6377589 to your computer and use it in GitHub Desktop.
Save lidi/6377589 to your computer and use it in GitHub Desktop.
def title_case(title, options = {})
return title if title.empty?
head, *tail = title.split(' ')
head.capitalize! << ' ' << tail.collect { |x| options[:minors] && options[:minors].downcase.split(' ').include?(x) ? x : x.capitalize! }.join(' ')
end
=====================
TESTS
Test.expect(title_case('the quick brown fox') == 'The Quick Brown Fox')
Test.expect(title_case('a clash of KINGS', minors: 'a an the OF') == 'A Clash of Kings')
Test.expect(title_case('') == '')
@lidi
Copy link
Author

lidi commented Aug 29, 2013

If fact I might have overreacted with hash as optional parameter, so I've rewrote it:

def title_case(title, options = '')
return title if title.empty?

head, *tail = title.split(' ')

head.capitalize! << ' ' << tail.collect { |x| !options.empty? && options.downcase.split(' ').include?(x.downcase) ? x : x.capitalize! }.join(' ')

end

Tests are still passing but I get the same error when trying to submit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment