Bird
0
0

Given a user input string that may have spaces and mixed case, which Ruby code snippet safely normalizes it to lowercase without extra spaces?

hard📝 Application Q9 of 15
Ruby - String Operations
Given a user input string that may have spaces and mixed case, which Ruby code snippet safely normalizes it to lowercase without extra spaces?
Ainput.downcase!
Binput.strip.downcase
Cinput.strip! && input.downcase!
Dinput.downcase
Step-by-Step Solution
Solution:
  1. Step 1: Remove spaces first

    Use strip to remove spaces from start and end before changing case.
  2. Step 2: Convert to lowercase

    Apply downcase after stripping to get lowercase string without spaces.
  3. Step 3: Check other options

    input.downcase changes case but leaves spaces. input.strip! && input.downcase! uses bang methods which may return nil and cause errors. input.downcase! only downcases without removing spaces.
  4. Final Answer:

    input.strip.downcase -> Option B
  5. Quick Check:

    strip then downcase = clean lowercase input [OK]
Quick Trick: Strip spaces before downcase to avoid leftover spaces [OK]
Common Mistakes:
  • Using bang methods without checking nil
  • Changing case before stripping
  • Ignoring spaces in input

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes