Bird
0
0

You have a list of names with extra spaces and mixed cases: [" Alice", "bob ", "CHARLIE"]. Which Ruby code correctly returns a new list with all names trimmed and in uppercase?

hard📝 Application Q8 of 15
Ruby - String Operations
You have a list of names with extra spaces and mixed cases: [" Alice", "bob ", "CHARLIE"]. Which Ruby code correctly returns a new list with all names trimmed and in uppercase?
A[" Alice", "bob ", "CHARLIE"].map { |n| n.strip.upcase }
B[" Alice", "bob ", "CHARLIE"].map { |n| n.upcase }
C[" Alice", "bob ", "CHARLIE"].map(&:strip).map(&:downcase)
D[" Alice", "bob ", "CHARLIE"].map(&:upcase)
Step-by-Step Solution
Solution:
  1. Step 1: Understand the order of methods

    We must remove spaces first with strip, then convert to uppercase with upcase.
  2. Step 2: Analyze each option

    [" Alice", "bob ", "CHARLIE"].map { |n| n.strip.upcase } correctly strips spaces then upcases each name. [" Alice", "bob ", "CHARLIE"].map { |n| n.upcase } upcases first, leaving spaces. [" Alice", "bob ", "CHARLIE"].map(&:strip).map(&:downcase) strips then downcases (wrong case). [" Alice", "bob ", "CHARLIE"].map(&:upcase) upcases, leaving spaces.
  3. Final Answer:

    [" Alice", "bob ", "CHARLIE"].map { |n| n.strip.upcase } -> Option A
  4. Quick Check:

    strip then upcase in map = clean uppercase list [OK]
Quick Trick: Strip spaces before changing case for clean results [OK]
Common Mistakes:
  • Changing case before stripping spaces
  • Using multiple map calls unnecessarily
  • Assuming order doesn't matter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes