HOWTO · Ruby
How to Convert a String to Lowercase or Uppercase in Ruby
Learn how Ruby's downcase, upcase, downcase!, and upcase! methods convert strings, how they differ, and how to check a string's case.
On this page
Ruby provides downcase and upcase to return a string with its letters converted to lowercase or uppercase. Their bang counterparts, downcase! and upcase!, change the receiver when a conversion is needed. Choose the non-bang method when the original value should remain available; choose a bang method when changing that string is intentional.
Convert a string to lowercase with downcase
Call downcase on a string to get a lowercase version. The method returns a string and leaves the receiver unchanged, so you can keep using the original value or assign the result to a new variable. This is useful when normalizing input for a comparison while retaining the text as entered.
Run this example:
original = "Hello, Ruby!"
lowercase = original.downcase
puts lowercase
puts original
hello, ruby!
Hello, Ruby!
Ruby also applies case mappings to supported non-ASCII characters in strings. For example, with a UTF-8 string, "Ä".downcase produces "ä". Case mappings are language data mappings, not a promise of locale-specific spelling or collation rules; use a locale-aware library when an application requires those rules.
Convert a string to uppercase with upcase
Use upcase for the corresponding uppercase conversion. Like downcase, it returns the converted string without changing the receiver. Assign the return value when later code should use the uppercase form, and keep the original variable when its existing capitalization matters.
original = "Hello, Ruby!"
uppercase = original.upcase
puts uppercase
puts original
HELLO, RUBY!
Hello, Ruby!
The two methods leave non-letter characters, such as spaces and punctuation, in place. They change letter case; they do not trim whitespace or otherwise normalize a string.
Choose between returning a new string and modifying in place
Use downcase! or upcase! when the receiver itself should change. A bang method returns the modified string when it makes a change. If the receiver already has the requested case, it returns nil because there was no change. Do not rely on a bang method always returning a string.
word = "Ruby"
changed = word.downcase!
puts word
puts changed
already_lowercase = "ruby"
result = already_lowercase.downcase!
puts result.inspect
ruby
ruby
nil
The last line intentionally displays the return value with inspect, so the no-change result is visible as nil. The string remains usable either way: after a successful bang conversion, it contains the converted text; when no conversion is needed, it remains as it was. These methods are about mutation behavior. This article makes no speed or memory guarantee, so choose between them based on whether changing the existing object fits the surrounding code.
Check whether a string is already lowercase or uppercase
Compare a string with the result of converting it. Equality with downcase means the string is already lowercase according to Ruby’s mapping; equality with upcase checks the corresponding uppercase form. This is a direct way to validate a case requirement without changing the original value.
value = "ruby"
puts(value == value.downcase)
puts(value == value.upcase)
true
false
This comparison checks case only. It does not remove leading or trailing spaces, and it does not make two strings equivalent under every language’s collation rules. If input also needs trimming or locale-aware comparison, handle those requirements separately and deliberately.