String#upcase with Unicode Options
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.
That makes it a small but dependable tool whenever text needs a consistent presentation. It is also easy to chain with other string methods because the original value remains untouched.
In practice, that makes upcase a good fit for labels, prompts, and quick comparisons where the source text should stay available for later use. The method does one job and keeps the rest of the code simple.
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. These flags give you precise control over which characters are affected, which matters when the text contains a mix of scripts.
Basic Usage
"hello".upcase
# => "HELLO"
"Hello, Ruby!".upcase
# => "HELLO, RUBY!"
"123 abc".upcase
# => "123 ABC"
The examples above show that non-alphabetic characters pass through unchanged. Numbers, spaces, and punctuation stay exactly as they were, which is important when you are normalising user input or formatting display text without accidentally altering the structure.
The original string is not modified:
name = "ruby"
upper = name.upcase
name
# => "ruby"
upper
# => "RUBY"
Because upcase returns a new string, the original value stays available if you need it later. This is the safer default when you are passing the result into another method or displaying the capitalised version alongside the original input.
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.
That expansion is normal, and it is one reason why uppercase conversion is best treated as a formatting step rather than a byte-preserving transform. The output is still correct, but its length may change.
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.
Keeping the conversion narrow can be useful when you are dealing with legacy formats or token-like values. It avoids changing letters outside the ASCII range.
Turkic case mapping
Turkish and other Turkic languages distinguish dotted and dotless I. Use :turkic when that distinction matters:
"Türkiye".upcase
# => "TÜRKIYE"
Without `:turkic`, Ruby uses the default Unicode mapping, which is usually correct for English text but not always for Turkish text.
"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.
That option matters because dotted and dotless I are distinct letters in Turkic languages. When the text needs to respect that distinction, the explicit option keeps the result accurate.
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
upcase! modifies the receiver in place, which avoids allocating a new string. Use it when you are certain you no longer need the original casing and want to save the memory allocation.
Use upcase! when you want to mutate the receiver:
word = "ruby"
word.upcase!
# => "RUBY"
word
# => "RUBY"
upcase! returns nil if no changes were made. This is the standard Ruby convention for bang methods: return self when modified, return nil when nothing changed. The nil return gives you a way to branch on whether the operation had any effect.
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. The non-bang version also integrates better with method chains and functional-style pipelines where each step produces a fresh value.
Common patterns
Normalize user input
def yes?(input)
input.to_s.upcase == "YES"
end
yes?("yes")
# => true
Uppercasing user input is a simple way to make comparisons more forgiving without changing the original text permanently. It keeps the check short and easy to reuse.
This pattern is especially handy for command flags, yes-or-no prompts, and other short responses. It keeps the comparison rule in one place instead of spreading case handling across multiple branches.
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