Created
January 30, 2014 16:06
-
-
Save billhorsman/8711993 to your computer and use it in GitHub Desktop.
Correcting case in a name
This file contains 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 User | |
attr_accessor :first_name, :last_name | |
def name | |
out = [first_name, last_name].join(' ').strip | |
out =~ /\A([a-z\s'\-]*|[A-Z\s'\-]*)\Z/ ? out.titleize : out | |
end | |
end | |
# Too lazy to use the SHIFT key? Don't worry, we'll do that for you. | |
"alice" + "marigold" => "Alice Marigold" | |
# Angry? We'll tone that down. | |
"ALICE" + "MARIGOLD" => "Alice Marigold" | |
# Already know how to capitalize your name? That's fine, we'll leave it alone. | |
"Alice" + "du Marigold" => "Alice du Marigold" | |
# Another example where we'll leave your name alone. | |
"Alice" + "O'Conner" => "Alice O'Conner" | |
# This one fails a little, but not too bad. | |
"alice " + "o'conner" => "Alice O'conner" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment