These methods help change or clean text easily. You can make all letters uppercase, lowercase, or remove extra spaces.
String methods (upcase, downcase, strip) in Ruby
Introduction
Syntax
Ruby
string.upcase
string.downcase
string.stripupcase makes all letters uppercase.
downcase makes all letters lowercase.
strip removes spaces at the start and end of the string.
Examples
Ruby
"hello".upcase # => "HELLO"
Ruby
"WORLD".downcase # => "world"
Ruby
" ruby ".strip # => "ruby"
Sample Program
This program shows how the three methods change the string name. It prints the original string, then the uppercase, lowercase, and stripped versions.
Ruby
name = " Alice " puts "Original: '#{name}'" puts "Upcase: '#{name.upcase}'" puts "Downcase: '#{name.downcase}'" puts "Strip: '#{name.strip}'"
Important Notes
These methods do not change the original string unless you add an exclamation mark, like upcase!.
Use strip to clean user input before processing.
Summary
upcase makes all letters uppercase.
downcase makes all letters lowercase.
strip removes spaces at the start and end of a string.
Practice
1. What does the Ruby method
upcase do to a string?easy
Solution
Step 1: Understand the method name
The methodupcasesuggests making letters uppercase.Step 2: Recall the method effect on strings
upcasechanges all letters to uppercase, not lowercase or trimming spaces.Final Answer:
Converts all letters in the string to uppercase -> Option BQuick Check:
upcase = uppercase letters [OK]
Hint: Remember: upcase = uppercase letters [OK]
Common Mistakes:
- Confusing upcase with downcase
- Thinking upcase removes spaces
- Assuming upcase reverses the string
2. Which of the following is the correct syntax to remove spaces from the start and end of a string
str in Ruby?easy
Solution
Step 1: Identify the method to remove spaces
The Ruby method to remove spaces at start and end isstrip, called on the string object.Step 2: Check correct method call syntax
In Ruby, methods are called with dot notation:str.strip(). There is notrimmethod in Ruby.Final Answer:
str.strip() -> Option AQuick Check:
strip method called with dot = str.strip() [OK]
Hint: Use dot notation: string.strip() removes spaces [OK]
Common Mistakes:
- Using strip as a standalone function
- Using trim instead of strip
- Calling upcase to remove spaces
3. What is the output of the following Ruby code?
str = " Hello World " puts str.strip.upcase
medium
Solution
Step 1: Apply
stripto the stringstripremoves spaces at the start and end, so " Hello World " becomes "Hello World".Step 2: Apply
upcaseto the stripped stringupcaseconverts all letters to uppercase, so "Hello World" becomes "HELLO WORLD".Final Answer:
"HELLO WORLD" -> Option DQuick Check:
strip + upcase = "HELLO WORLD" [OK]
Hint: strip removes spaces, upcase makes uppercase [OK]
Common Mistakes:
- Forgetting strip removes spaces
- Applying upcase before strip
- Expecting original spaces to remain
4. The following Ruby code is intended to convert a string
name to lowercase and remove spaces at the start and end. What is wrong?name = " RubyLang " name.downcase.strip
medium
Solution
Step 1: Check method calls and order
Bothdowncaseandstripare valid methods and can be chained in any order.Step 2: Check if result is used
The code calls methods but does not save or print the result, so the original string remains unchanged.Final Answer:
The result is not saved or printed -> Option CQuick Check:
Method calls without assignment or output do nothing visible [OK]
Hint: Always save or print method results to see changes [OK]
Common Mistakes:
- Thinking method order matters here
- Assuming methods change string in place
- Confusing strip with a non-existent method
5. You have a list of names with inconsistent spacing and letter cases:
Which Ruby code correctly cleans this list by removing spaces and making all names lowercase?
names = [" Alice", "BOB ", " cHaRlie "]
Which Ruby code correctly cleans this list by removing spaces and making all names lowercase?
hard
Solution
Step 1: Understand the goal
We want to remove spaces and convert all names to lowercase.Step 2: Check each option
clean_names = names.map { |n| n.strip.downcase } usesstripthendowncase, which removes spaces first, then lowercases. clean_names = names.map { |n| n.downcase.chomp } uses chomp after downcase, but chomp removes only trailing newlines, not spaces, so spaces remain. clean_names = names.map { |n| n.strip.upcase } usesupcasewhich is wrong. clean_names = names.map { |n| n.downcase } does not remove spaces.Step 3: Choose best practice
The code that usesstripto remove whitespace anddowncaseto lowercase correctly cleans the names.Final Answer:
clean_names = names.map { |n| n.strip.downcase } -> Option AQuick Check:
strip then downcase cleans names correctly [OK]
Hint: Strip spaces first, then change case for clean strings [OK]
Common Mistakes:
- Using upcase instead of downcase
- Confusing chomp with strip
- Forgetting to map over the list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
