Last active
August 29, 2015 14:24
-
-
Save ivorpad/84f2e12570b94e55c4eb to your computer and use it in GitHub Desktop.
Loops: Title Case - Bloc.io: Full Stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Title | |
| attr_reader :str | |
| def initialize(str) | |
| @str = str | |
| end | |
| def fix | |
| common_words = ["the", "of", "a", "on", "in", "for", "by"] | |
| fixed = [] | |
| str.downcase.split.each_with_index do |word, index| | |
| if index > 0 && common_words.include?(word) | |
| fixed << word | |
| else | |
| fixed << word.capitalize | |
| end | |
| end | |
| fixed.join(" ") | |
| end | |
| end | |
| my_title = Title.new("a title OF A book") | |
| p my_title.fix |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment