Bird
0
0

Examine the Ruby code below. What is the cause of the error?

medium📝 Debug Q6 of 15
Ruby - String Operations
Examine the Ruby code below. What is the cause of the error?
sentence = "one two three"
words = sentence.split(" ")
joined = words.join("-")
puts joined.length
AError because <code>join</code> requires an array, but <code>words</code> is a string
BError because <code>split</code> cannot take a space as an argument
CNo error; the code runs and outputs the length of the joined string
DError because <code>puts</code> cannot output the length of a string
Step-by-Step Solution
Solution:
  1. Step 1: Analyze split usage

    split(" ") splits the string into an array of words separated by spaces.
  2. Step 2: Analyze join usage

    join("-") combines the array elements into a single string separated by dashes.
  3. Step 3: Analyze puts joined.length

    joined.length returns the length of the resulting string, which puts outputs.
  4. Final Answer:

    No error; the code runs and outputs the length of the joined string -> Option C
  5. Quick Check:

    split returns array, join combines array, length returns string size. [OK]
Quick Trick: split returns array, join combines, length returns string size [OK]
Common Mistakes:
  • Assuming split cannot take a space as delimiter
  • Thinking join requires a string, not an array
  • Believing puts cannot output numeric values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes