Bird
0
0

You want to create a simple Ruby program that prints each word from an array on a new line. Which code correctly does this?

hard📝 Application Q15 of 15
Ruby - Basics and Runtime
You want to create a simple Ruby program that prints each word from an array on a new line. Which code correctly does this?
Awords = ("Ruby", "is", "fun") for word in words puts word end
Bwords = ["Ruby", "is", "fun"] print words.join("\n")
Cwords = ["Ruby", "is", "fun"] words.each { |word| puts word }
Dwords = {"Ruby", "is", "fun"} words.each do |word| print word end
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct array syntax

    words = ["Ruby", "is", "fun"] words.each { |word| puts word } uses square brackets [] to create an array, which is correct in Ruby.
  2. Step 2: Check iteration and printing method

    words = ["Ruby", "is", "fun"] words.each { |word| puts word } uses each with a block and puts to print each word on a new line, which is the correct approach.
  3. Final Answer:

    words = ["Ruby", "is", "fun"]\nwords.each { |word| puts word } -> Option C
  4. Quick Check:

    Array + each + puts = print words line by line [OK]
Quick Trick: Use array.each with puts to print each item [OK]
Common Mistakes:
  • Using parentheses instead of brackets for arrays
  • Using print instead of puts for new lines
  • Using curly braces for arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes