Bird
0
0

You have a string str = " Ruby ". Which code snippet correctly removes leading and trailing spaces and modifies str itself?

hard📝 Application Q8 of 15
Ruby - Methods
You have a string str = " Ruby ". Which code snippet correctly removes leading and trailing spaces and modifies str itself?
Astr.strip!
Bstr.strip
Cstr.strip!()
Dstr.strip = true
Step-by-Step Solution
Solution:
  1. Step 1: Identify method to remove spaces

    The strip method removes leading and trailing spaces from a string.
  2. Step 2: Choose method that modifies original string

    The bang method strip! modifies the original string in place.
  3. Final Answer:

    str.strip! -> Option A
  4. Quick Check:

    Bang method strip! modifies original string [OK]
Quick Trick: Use strip! to change string in place [OK]
Common Mistakes:
  • Using non-bang strip which returns new string
  • Adding unnecessary parentheses
  • Trying to assign strip as a property

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes