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:
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.
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.
Final Answer:
"Hello, #{name}\nWelcome!" -> Option B
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
Master "Variables and Data Types" in Ruby
9 interactive learning modes - each teaches the same concept differently