Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

String#capitalize!

str.capitalize! → str or nil
str.capitalize!([options]) → str or nil

Modifies str by converting the first character to uppercase and the remainder to lowercase. Returns nil if no changes are made. There is an exception for modern Georgian (mkhedruli/MTAVRULI), where the result is the same as for String#downcase, to avoid mixed case.

See String#downcase for meaning of options and use with different encodings.

Example
a = "hello"
a.capitalize!   #=> "Hello"
a               #=> "Hello"
a.capitalize!   #=> nil
See also #capitalize #casecmp #byteslice #casecmp? also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1