String#upcase
str.upcase(*options) → string String#upcase returns a new string with its characters converted to uppercase. It is useful for display formatting, normalization, and simple case-insensitive comparisons where you want to leave the original string unchanged.
Syntax
str.upcase
str.upcase(*options)
upcase accepts optional case-mapping flags. The most common ones are :ascii, which limits conversion to ASCII letters, and :turkic, which applies Turkic dotted/dotless I rules.
Basic Usage
"hello".upcase
# => "HELLO"
"Hello, Ruby!".upcase
# => "HELLO, RUBY!"
"123 abc".upcase
# => "123 ABC"
The original string is not modified:
name = "ruby"
upper = name.upcase
name
# => "ruby"
upper
# => "RUBY"
Unicode Case Mapping
Ruby uses Unicode-aware case mapping by default for supported encodings, so non-ASCII letters can change too:
"über".upcase
# => "ÜBER"
"straße".upcase
# => "STRASSE"
"Ångström".upcase
# => "ÅNGSTRÖM"
Some characters expand to multiple characters when uppercased. For example, German ß becomes SS, so the result can be longer than the original string.
ASCII-Only Conversion
Use :ascii when you only want a through z converted:
"über café".upcase(:ascii)
# => "üBER CAFé"
This is useful for protocols, identifiers, or data formats where only ASCII case rules should apply.
Turkic Case Mapping
Turkish and other Turkic languages distinguish dotted and dotless I. Use :turkic when that distinction matters:
"Türkiye".upcase
# => "TÜRKIYE"
"Türkiye".upcase(:turkic)
# => "TÜRKİYE"
Without :turkic, Ruby uses the default Unicode mapping, which is usually correct for English text but not always for Turkish text.
Return Value
upcase always returns a string. The returned string is a separate object, even if no characters changed:
word = "RUBY"
result = word.upcase
result
# => "RUBY"
result.equal?(word)
# => false
In-Place Uppercasing
Use upcase! when you want to mutate the receiver:
word = "ruby"
word.upcase!
# => "RUBY"
word
# => "RUBY"
upcase! returns nil if no changes were made:
word = "RUBY"
word.upcase!
# => nil
Prefer upcase unless mutation is intentional. It is easier to reason about code when string transformations return new values instead of changing existing objects.
Common Patterns
Normalize User Input
def yes?(input)
input.to_s.upcase == "YES"
end
yes?("yes")
# => true
Format Labels
labels = ["name", "email", "country"]
labels.map(&:upcase)
# => ["NAME", "EMAIL", "COUNTRY"]
See Also
- String#downcase — convert characters to lowercase
- String#swapcase — switch uppercase characters to lowercase and lowercase characters to uppercase