0
0
Rubyprogramming

String creation (single and double quotes) in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
How do you create a string using single quotes in Ruby?
You create a string by enclosing text within single quotes, like 'Hello'. Single quotes treat the content literally without interpreting escape sequences or variables.
Click to reveal answer
beginner
How do double quotes differ from single quotes when creating strings in Ruby?
Double quotes allow escape sequences (like \n for new line) and variable interpolation (inserting variable values inside the string). Single quotes do not.
Click to reveal answer
beginner
What will this Ruby code output? puts 'Hello\nWorld'
It will output exactly: Hello\nWorld (with the backslash and n shown), because single quotes do not interpret escape sequences.
Click to reveal answer
beginner
What will this Ruby code output? puts "Hello\nWorld"
It will output: Hello (new line) World, because double quotes interpret \n as a new line character.
Click to reveal answer
beginner
How do you insert a variable inside a string using double quotes in Ruby?
Use #{variable_name} inside double quotes. Ruby replaces it with the variable's value. For example: name = 'Sam'; puts "Hello, #{name}!" outputs Hello, Sam!
Click to reveal answer
Which type of quotes in Ruby allow variable interpolation inside strings?
ANeither single nor double quotes
BSingle quotes
CBoth single and double quotes
DDouble quotes
What will this Ruby code print? puts 'Line1\nLine2'
ALine1 (new line) Line2
BLine1\nLine2
CLine1Line2
DError
Which of these is a correct way to create a string with a new line in Ruby?
A'Hello\nWorld'
B"Hello\nWorld"
C"Hello World"
D"dlroWn\olleH"
What does this Ruby code output? name = 'Amy'; puts "Hi, #{name}!"
AHi, name!
BHi, #{name}!
CHi, Amy!
DError
Which quote style should you use if you want the string to be exactly as typed, without interpreting escape sequences?
ASingle quotes
BBackticks
CDouble quotes
DNo quotes
Explain the difference between single and double quotes when creating strings in Ruby.
Think about how Ruby handles \n and #{variable} inside strings.
You got /4 concepts.
    How would you include a variable's value inside a string in Ruby? Give an example.
    Remember that single quotes do not support interpolation.
    You got /3 concepts.