Bird
0
0

Which of the following is the correct syntax to split a string "one,two,three" by commas in Ruby?

easy📝 Syntax Q3 of 15
Ruby - String Operations
Which of the following is the correct syntax to split a string "one,two,three" by commas in Ruby?
A"one,two,three".join()
B"one,two,three".join(',')
C"one,two,three".split()
D"one,two,three".split(',')
Step-by-Step Solution
Solution:
  1. Step 1: Understand split syntax

    The split method takes a string delimiter as an argument to divide the string at that delimiter.
  2. Step 2: Verify correct usage

    "one,two,three".split(',') correctly calls split with ',' to split by commas. Options B and D use join incorrectly on a string, and C splits without specifying delimiter which defaults to whitespace.
  3. Final Answer:

    "one,two,three".split(',') -> Option D
  4. Quick Check:

    split with ',' splits string by commas [OK]
Quick Trick: Use split(',') to split by commas [OK]
Common Mistakes:
  • Using join instead of split
  • Omitting delimiter when needed
  • Calling join on strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes