Last active
August 29, 2015 14:28
-
-
Save kangkyu/239668f8970d77cb40f6 to your computer and use it in GitHub Desktop.
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
def proper(phrase) | |
words = phrase.split(" ") | |
words.map {|word| cap_if_selected(word)}.join(" ") | |
end | |
def cap_if_selected(word) | |
word = word.each_char.map {|char| down_char(char)}.join | |
word[0] = up_char(word[0]) if selected(word) | |
word | |
end | |
def selected(word) | |
word.length > 3 | |
end | |
def up_char(char) | |
up_hash = Hash[('a'..'z').zip('A'..'Z')] | |
char.match(/[a-z]/) ? up_hash[char] : char | |
end | |
def down_char(char) | |
down_hash = Hash[('a'..'z').zip('A'..'Z')].invert | |
char.match(/[A-Z]/) ? down_hash[char] : char | |
end | |
p proper("word Of MOuth") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍