Skip to content

Instantly share code, notes, and snippets.

@kangkyu
Last active August 29, 2015 14:28
Show Gist options
  • Save kangkyu/239668f8970d77cb40f6 to your computer and use it in GitHub Desktop.
Save kangkyu/239668f8970d77cb40f6 to your computer and use it in GitHub Desktop.
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")
@charliemcelfresh
Copy link

👍

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