Bird
0
0

How can you create a string that includes a variable's value and a newline in Ruby?

hard📝 Application Q9 of 15
Ruby - Variables and Data Types
How can you create a string that includes a variable's value and a newline in Ruby?
A'Hello, #{name}\nWelcome!'
B"Hello, #{name}\nWelcome!"
C"Hello, #{name}\nWelcome!".length
D'Hello, ' + name + '\nWelcome!'
Step-by-Step Solution
Solution:
  1. Step 1: Use double quotes for interpolation and escape sequences

    Double quotes allow #{name} to be replaced by variable value and \n to create newline.
  2. Step 2: Check options for correct usage

    "Hello, #{name}\nWelcome!" uses double quotes with interpolation and newline correctly. A uses single quotes, so no interpolation or newline. C calls .length producing an integer instead of string. D concatenates strings but \n is literal, not newline.
  3. Final Answer:

    "Hello, #{name}\nWelcome!" -> Option B
  4. Quick Check:

    Double quotes needed for interpolation and escape sequences [OK]
Quick Trick: Use double quotes for interpolation and escape sequences [OK]
Common Mistakes:
MISTAKES
  • Using single quotes for interpolation
  • Expecting \n to work in single quotes
  • Unnecessary to_s calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes