Skip to content
instance method

String#capitalize

() -> ::String
(:ascii | :lithuanian | :turkic) -> ::String
(:lithuanian, :turkic) -> ::String
(:turkic, :lithuanian) -> ::String
capitalize(mapping = :ascii) → new_string

Returns a string containing the characters in self, each with possibly changed case:

  • The first character made uppercase.

  • All other characters are made lowercase.

Examples:

Example
'hello'.capitalize  # => "Hello"
'HELLO'.capitalize  # => "Hello"
'straße'.capitalize # => "Straße"  # Lowercase 'ß' not changed.
'STRAẞE'.capitalize # => "Straße"  # Uppercase 'ẞ' downcased to 'ß'.
'привет'.capitalize # => "Привет"
'ПРИВЕТ'.capitalize # => "Привет"

Some characters (and some character sets) do not have upcase and downcase versions; see Case Mapping:

Example
s = '1, 2, 3, ...'
s.capitalize == s # => true
s = 'こんにちは'
s.capitalize == s # => true

The casing is affected by the given mapping, which may be :ascii, :fold, or :turkic; see Case Mappings.

Related: see Converting to New String.

Ruby version changes

  1. 3.0
See also #capitalize! #bytesplice #byteslice #casecmp also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0