String#capitalize
str.capitalize -> string String#capitalize returns a new text with the first character converted to uppercase and all remaining characters converted to lowercase. It is useful for formatting names, titles, and other text that should start with a capital letter.
This method does not modify the original string. It always returns a fresh string object.
Because it normalizes the rest of the text too, capitalize is more than a simple first-letter tweak. It is a formatting method that can make inconsistent input look tidy, but it can also change letters you might have wanted to keep as-is.
That tradeoff is why capitalize is best for display text instead of identifiers. If you are normalizing names from a form field, it can save a cleanup step. If you are working with codes, acronyms, or case-sensitive tokens, the method may do more than you want.
Syntax
string.capitalize
Parameters
This method takes no parameters.
Because capitalize accepts no arguments, the behaviour is always the same: uppercase the first character, lowercase everything else. That consistency makes the method easy to reason about since there are no flags or options that change how the transformation works.
Examples
Basic usage
"hello".capitalize
# => "Hello"
"world".capitalize
# => "World"
The output of capitalize always has exactly one uppercase letter at the front, regardless of how many capital letters appeared in the original input. That means the method does two things at once: it uppercases the first character and lowercases everything that follows.
All remaining characters become lowercase
Every character after the first gets forced to lowercase, regardless of its original case:
"HELLO".capitalize
# => "Hello"
"hELLO".capitalize
# => "Hello"
"HeLLo WoRLD".capitalize
# => "Hello world"
Notice that the space and the second word lose their capital letters too. capitalize treats the entire remainder of the text the same way, so "HeLLo WoRLD" becomes "Hello world" with only the very first H kept uppercase. This wholesale lowercasing of the tail is what makes the method a formatting tool rather than a surgical one.
Non-alphabetic first character
If the first character is not an ASCII letter, no uppercasing occurs. Only the rest of the string gets lowercased:
"123abcDEF".capitalize
# => "123abcdef"
"!hello".capitalize
# => "!hello"
When the first character is a digit or a symbol, Ruby skips the uppercase step entirely because there is no letter to promote. The rest of the text still gets lowercased, which can be surprising when the input mixes numbers and letters in the middle. That is worth remembering when you process identifiers or codes that might start with a non-letter.
Empty string
"".capitalize
# => ""
An empty string returns an empty string with no error, so you can safely call capitalize on values that might be blank without guarding against it first. That makes the method easy to chain in pipelines where some inputs are optional or where the source data occasionally produces empty fields.
Unicode and case mapping options
Modern Ruby applies Unicode-aware case mapping by default. That means non-ASCII letters can be capitalized correctly when the string encoding supports them.
"étude".capitalize
# => "Étude"
"über".capitalize
# => "Über"
Modern Ruby handles accented characters correctly, so "étude" becomes "Étude" with the leading accent preserved on the capital letter. This Unicode-aware behaviour means you can format names and words from languages that use diacritics without losing the marks.
If you specifically want ASCII-only behavior, pass the :ascii option:
"über".capitalize(:ascii)
# => "über"
The :ascii option skips the Unicode case-mapping tables and only converts the basic Latin letters A-Z and a-z. This is useful when you are processing protocol tokens, legacy file formats, or identifiers where non-ASCII characters should stay exactly as they are.
Use the default behavior for normal text. Reach for :ascii only when you are intentionally processing ASCII-only identifiers, protocol values, or legacy data.
In other words, the method is safe for everyday presentation work, but not for every string problem. The more the input behaves like data instead of prose, the more carefully you should think about whether to capitalize it at all.
Mutating version
String#capitalize! modifies the string in place instead of returning a new one:
name = "alice"
name.capitalize!
# => "Alice"
name
# => "Alice"
The bang method modifies the receiver in place, which is useful when you want to avoid creating extra string objects in a tight loop. After capitalize! runs, the original variable holds the transformed value, so any code that reads it later sees the change.
Note that capitalize! returns nil if no change was needed:
str = "Alice"
str.capitalize!
# => nil
str
# => "Alice"
The nil return when no change is needed is a standard Ruby convention for bang methods. It lets you write conditional logic that only acts when the text actually changed, which is handy in update loops where you want to log or respond to modifications without extra checks.
common patterns
Formatting user input
A common pattern is to combine capitalize with other string methods:
def format_name(name)
name.strip.capitalize
end
format_name(" john doe ")
# => "John doe"
This converts "john doe" to "John doe" — the first letter becomes uppercase and everything else becomes lowercase.
That behavior is usually what you want for a single display field, especially when the source data is messy or comes from user input. If you need to preserve the rest of the case, you will need a different approach because capitalize always rewrites the trailing characters too.
Working with mixed-case input
"mACDONALD".capitalize
# => "Macdonald"
"o'BRIEN".capitalize
# => "O'brien"
Names with internal capitals like MacDonald or O'Brien lose their mid-word capitalisation because capitalize forces everything after the first letter to lowercase. For proper name formatting you would need a more specialised approach.
Capitalising each word separately
When you want every word to start with a capital letter rather than just the first, chain split, map, and join:
def titlecase(str)
str.split.map(&:capitalize).join(" ")
end
titlecase("john doe")
# => "John Doe"
titlecase("mary jane smith")
# => "Mary Jane Smith"
This pattern handles multi-word input by capitalising each word independently, which is closer to what you would expect for a display name or a title. It works well for simple name fields and single-line input.
Chaining with other string methods
capitalize works well in method chains for cleaning up user input before storage:
def clean_name(input)
input.strip.downcase.capitalize
end
clean_name(" ALICE ")
# => "Alice"
clean_name(" BOB ")
# => "Bob"
The method chain reads naturally from left to right: strip the whitespace, normalise the case, then capitalise the first letter. Each step handles one concern, and the result is predictable regardless of how messy the original input was. This pattern appears often in form handlers and data import scripts.
" CHARLIE ".strip.capitalize
# => "Charlie"
Downcasing first guarantees a consistent starting point, and capitalize then promotes the first letter. The chain reads left to right as a series of normalisation steps, each method doing one small job before handing off to the next. That clarity is what makes method chaining feel natural in Ruby.
# Works on frozen strings too
"hello".freeze.capitalize
# => "Hello"