Skip to content

Instantly share code, notes, and snippets.

@billhorsman
Created January 30, 2014 16:06
Show Gist options
  • Save billhorsman/8711993 to your computer and use it in GitHub Desktop.
Save billhorsman/8711993 to your computer and use it in GitHub Desktop.
Correcting case in a name
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