rubyguides

String#upcase

str.upcase -> string

String#upcase returns a new string with all characters converted to uppercase. It is useful for emphasizing text, normalizing input, and formatting output for display.

This method does not modify the original string—it always returns a new string.

Syntax

string.upcase

Parameters

This method takes no parameters. It operates on all characters in the string.

The method works on the whole input at once with no options to configure, so the call site stays short and the behaviour is always predictable. Every alphabetic character gets its uppercase form regardless of where it sits in the text.

Examples

Basic usage

"hello world".upcase
# => "HELLO WORLD"

"ruby programming".upcase
# => "RUBY PROGRAMMING"

Both examples produce the expected uppercase output with no surprises. The method works on the entire input at once, so multi-word input gets the same treatment as a single word. There is no per-word logic or special handling for whitespace—every alphabetic character simply gets its uppercase equivalent.

With Unicode characters

"çàâ".upcase
# => "ÇÀÂ"

"über".upcase
# => "ÜBER"

"ångström".upcase
# => "ÅNGSTRÖM"

Ruby maps accented characters to their uppercase forms correctly, including letters with diacritics, umlauts, and the Swedish ring. This Unicode support means you can normalise text from multiple languages without losing the marks that carry meaning in those writing systems.

Working with special characters

"numbers: 123".upcase
# => "NUMBERS: 123"

"symbols: !@#".upcase
# => "SYMBOLS: !@#"

Digits and punctuation pass through unchanged because they have no uppercase equivalent. That means you can safely call upcase on values that mix letters with numbers or symbols without worrying about data loss in the non-alphabetic parts. This makes the method ideal for cleaning mixed-content input before display.

Common Patterns

Emphasis and constants

status = "active"
status.upcase
# => "ACTIVE"

# Common Ruby constant naming
module MyConstants
  DEFAULT_OPTIONS = {
    mode: "debug".upcase
  }
end

Storing an uppercased value in a constant is a common Ruby idiom for configuration keys, environment names, and lookup tables. The upcase call inside the constant definition keeps the source value readable while storing it in the canonical form that the rest of the codebase expects. This pattern is especially common in modules that define status enums or feature flags.

Conditional formatting

def format_status(active)
  active ? "ACTIVE" : "inactive"
end

format_status(true)
# => "ACTIVE"

format_status(false)
# => "inactive"

Using upcase inside a conditional lets you return a formatted label without mutating the underlying data. The method call creates a fresh uppercase string only when the condition is true, so the false branch can return a different casing without any extra cleanup.

Enum-like status codes

def normalize_status(status)
  status.upcase
end

normalize_status("pending")
# => "PENDING"

Normalising a status value to uppercase before comparison or storage is a small step that prevents case-sensitivity bugs. When status codes come from user input, an API, or a database column that was not normalised, a quick upcase makes the later checks consistent regardless of the original casing.

Unicode Support

Ruby handles Unicode properly. The method respects Unicode character properties, so it works correctly with international text. The Turkish dotted and dotless i problem is handled correctly in Ruby 2.5+.

using uppercase for emphasis and normalization

upcase is useful when the code wants to normalize text for comparison or present a value with stronger visual emphasis. It keeps the original string untouched, which makes it a safe choice for formatting and validation steps that should not mutate input. That matters in code that stores the original text for later use but still needs a consistent uppercase version for display or matching.

Because the method returns a new string, the surrounding code can decide whether to keep the uppercase value, compare against it, or simply use it for a label. That separation keeps the transformation easy to reason about and avoids mixing presentation with storage.

It also makes quick normalization steps very readable when the code wants to compare values without worrying about their original case. A short uppercase transform can be enough to make a lookup or a display label consistent. That keeps the method useful both for display work and for small validation tasks.

Case-insensitive comparison

def matches?(input, expected)
  input.upcase == expected.upcase
end

matches?("hello", "HELLO")
# => true

matches?("World", "world")
# => true

Normalising both sides before comparison is a simple way to make lookups case-insensitive without reaching for a regex or a more complex comparison library.

The method is simple, but that simplicity is the reason it shows up so often. It gives the code one obvious place to say, “I want this in uppercase now,” and then hands back a fresh string with no extra side effects. That clarity is what makes it feel right for labels, comparisons, and small formatting jobs.

See Also