Question
How can I take a string and convert it to lowercase or uppercase in Ruby?
Short Answer
By the end of this page, you will understand how Ruby changes string letter case using built-in methods like downcase and upcase. You will also learn the difference between methods that return a new string and methods that modify the original string.
Concept
Ruby provides built-in string methods for changing text case.
The two most common methods are:
downcase→ returns a lowercase version of a stringupcase→ returns an uppercase version of a string
These methods matter because text normalization is a very common task in programming. For example, you may want to:
- compare user input without worrying about capitalization
- store consistent values in a database
- format display text
- clean data before filtering or searching
In Ruby, strings are objects, and these methods are called directly on a string.
"Hello".downcase
"Hello".upcase
A very important detail is that Ruby often provides two versions of a string method:
- a non-destructive version like
downcaseorupcase - a destructive version like
downcase!orupcase!
The non-destructive version returns a new string and leaves the original unchanged. The destructive version tries to modify the original string object.
Understanding that difference is useful in real programs because it affects whether your variables keep their old values or get changed in place.
Mental Model
Think of a string like a printed label.
downcaseandupcasemake a copy of the label with different letter casingdowncase!andupcase!try to rewrite the original label itself
So if you have a box labeled Hello:
downcasegives you a new label:hellodowncase!changes the label already attached to the box
This helps explain why some methods return a new value while others change the existing object.
Syntax and Examples
Basic syntax
string.downcase
string.upcase
Example
text = "Hello World"
puts text.downcase
puts text.upcase
Output:
hello world
HELLO WORLD
In this example:
downcasereturns a lowercase copyupcasereturns an uppercase copytextitself is still unchanged
Original string stays the same
text = "Ruby"
lower = text.downcase
puts text # Ruby
puts lower # ruby
Modifying the original string
text = "Ruby"
text.downcase!
puts text # ruby
The ! version changes the original string directly.
Related useful method
Ruby also has , which makes the first character uppercase and the rest lowercase.
Step by Step Execution
Consider this example:
text = "HeLLo"
result = text.downcase
puts text
puts result
Step 1
text = "HeLLo"
The variable text refers to the string "HeLLo".
Step 2
result = text.downcase
Ruby calls downcase on text.
It creates a new string: "hello".
That new string is stored in result.
Step 3
puts text
This prints the original string:
HeLLo
Step 4
puts result
This prints the lowercase copy:
Real World Use Cases
Changing string case is common in many real programs.
User input normalization
email = "USER@EXAMPLE.COM"
normalized_email = email.downcase
This helps store emails consistently.
Case-insensitive comparison
answer = "Yes"
if answer.downcase == "yes"
puts "Confirmed"
end
This allows Yes, YES, and yes to be treated the same.
Formatting output
product_code = "abc123"
puts product_code.upcase
Useful when displaying codes, tags, or identifiers.
Cleaning imported data
When reading CSV files or API data, capitalization may be inconsistent. Converting values to a standard case makes searching and grouping easier.
Real Codebase Usage
In real Ruby codebases, case conversion is often part of validation, normalization, and comparison.
Normalizing before saving
Developers often normalize values before storing them:
user_email = params[:email].to_s.downcase
This avoids problems caused by mixed capitalization.
Guard clauses with normalization
role = params[:role].to_s.downcase
return "Invalid role" unless ["admin", "editor", "viewer"].include?(role)
This combines safe conversion and validation.
Searching and filtering
matches = names.select { |name| name.downcase.start_with?("a") }
This pattern helps make searches case-insensitive.
Config and environment values
mode = ENV["APP_MODE"].to_s.upcase
Teams may normalize environment strings before checking values.
Avoiding accidental mutation
In larger codebases, developers often prefer downcase over unless they specifically want to mutate the original string. This makes code easier to reason about and reduces side effects.
Common Mistakes
1. Expecting downcase to change the original string
Broken expectation:
text = "HELLO"
text.downcase
puts text
Output:
HELLO
Why: downcase returns a new string but does not modify text.
Fix:
text = "HELLO"
text = text.downcase
puts text
Or:
text = "HELLO"
text.downcase!
puts text
2. Forgetting that ! methods modify the object
text = "Hello"
text.upcase!
This changes text itself. That can be surprising if the value is still needed elsewhere.
3. Calling methods on nil
Broken code:
Comparisons
| Method | What it does | Changes original string? |
|---|---|---|
downcase | Returns a lowercase copy | No |
downcase! | Converts the original string to lowercase | Yes |
upcase | Returns an uppercase copy | No |
upcase! | Converts the original string to uppercase | Yes |
capitalize | Uppercases first letter, lowercases the rest | No |
capitalize! | Modifies original string using capitalize rules | Yes |
Non-destructive vs destructive
Cheat Sheet
Quick reference
"Hello".downcase # "hello"
"Hello".upcase # "HELLO"
"hELLO".capitalize # "Hello"
Destructive versions
text = "Hello"
text.downcase! # modifies text
text.upcase! # modifies text
Important rule
- Methods without
!usually return a new string - Methods with
!usually modify the original string
Common pattern
normalized = input.to_s.downcase
Useful when input might be nil.
Case-insensitive comparison
if value.to_s.downcase == "admin"
# do something
end
Watch out for
downcasedoes not change the original variable unless you reassign it
FAQ
How do I convert a string to lowercase in Ruby?
Use downcase:
"HELLO".downcase
How do I convert a string to uppercase in Ruby?
Use upcase:
"hello".upcase
What is the difference between downcase and downcase! in Ruby?
downcase returns a new string. downcase! modifies the original string.
Does upcase change the original string in Ruby?
No. upcase returns a new uppercase string unless you reassign it.
How can I safely lowercase a value that might be nil?
Use to_s first:
value.to_s.downcase
Should I use downcase! in Ruby?
Mini Project
Description
Build a small Ruby script that normalizes user input for a command-line program. The script should take a few mixed-case strings, print lowercase and uppercase versions, and perform a case-insensitive comparison. This demonstrates how case conversion is used in everyday Ruby programs.
Goal
Create a Ruby script that converts strings to lowercase and uppercase and uses normalized text for reliable comparisons.
Requirements
- Create at least one string with mixed capitalization.
- Print both the lowercase and uppercase versions of the string.
- Show the difference between
downcaseanddowncase!. - Perform one case-insensitive comparison using
downcase. - Keep the script runnable from the command line.
Keep learning
Related questions
How to Call Shell Commands from Ruby and Capture Output
Learn how to run shell commands in Ruby, capture output, check exit status, and choose the right method for scripts and apps.
How to Check Whether a String Contains a Substring in Ruby
Learn how to check if a string contains a substring in Ruby using include?, match, and multiline string examples.
How to Check if a Hash Key Exists in Ruby
Learn how to check whether a specific key exists in a Ruby hash using key?, has_key?, and include? with clear examples.