Bird
0
0

The following Ruby code is intended to split a string into words and then join them with a space, but it raises an error. What is the problem?

medium📝 Debug Q14 of 15
Ruby - String Operations
The following Ruby code is intended to split a string into words and then join them with a space, but it raises an error. What is the problem?
text = "hello world"
words = text.split
joined = words.join
puts joined()
AThe variable <code>words</code> is not an array.
BThe method <code>split</code> requires an argument.
CThe call <code>joined()</code> is incorrect syntax.
DThe <code>join</code> method cannot be called without a separator.
Step-by-Step Solution
Solution:
  1. Step 1: Check the method calls

    split without arguments splits on whitespace correctly. join can be called without arguments to join with empty string.
  2. Step 2: Identify the error in puts joined()

    In Ruby, calling a variable like a method with parentheses joined() causes an error because joined is a string, not a method.
  3. Final Answer:

    The call joined() is incorrect syntax. -> Option C
  4. Quick Check:

    Calling string like method with () = error [OK]
Quick Trick: Don't add () when printing variables in Ruby [OK]
Common Mistakes:
  • Thinking split needs an argument always
  • Assuming join requires a separator
  • Calling variables as methods with parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes