Single quotes create simple strings without special processing.
Double quotes allow special characters and variable insertion.
Examples
This creates a string with single quotes and prints it.
Ruby
name = 'Alice'puts name
This creates a string with double quotes and prints it.
Ruby
greeting = "Hello, world!"puts greeting
Double quotes let you insert variables inside the string using #{ }.
Ruby
age = 10puts"I am #{age} years old."
Use backslash to include a single quote inside a single-quoted string.
Ruby
puts'It\'s sunny today.'
Sample Program
This program shows how to create strings with single and double quotes, insert a variable, and use a newline.
Ruby
name = 'Bob'
greeting = "Hello, #{name}!"puts greeting
puts'This is a single-quoted string.'puts"This is a double-quoted string with a newline\nright here."
OutputSuccess
Important Notes
Single-quoted strings do not process escape sequences like \n (new line).
Double-quoted strings can include special characters like new lines and tabs.
Use #{variable} inside double quotes to add variable values directly.
Summary
Use single quotes for simple text without special characters.
Use double quotes when you need special characters or variable insertion.
Remember to escape quotes inside strings if needed.
Practice
(1/5)
1. Which of the following is the correct way to create a simple string with the text Hello World in Ruby?
easy
A. Using single quotes: 'Hello World'
B. Using double quotes: "Hello World"
C. Using backticks: `Hello World`
D. Using parentheses: (Hello World)
Solution
Step 1: Understand string creation in Ruby
Ruby allows strings to be created using single or double quotes. Single quotes are simplest for plain text.
Step 2: Identify the correct syntax for simple strings
Single quotes 'Hello World' correctly create a string without special processing.
Final Answer:
Using single quotes: 'Hello World' -> Option A
Quick Check:
Simple text string = single quotes [OK]
Hint: Use single quotes for plain text strings [OK]
Common Mistakes:
Using backticks which execute commands
Using parentheses which are not string delimiters
Confusing single and double quotes for simple strings
2. Which of the following is the correct syntax to include a newline character inside a string in Ruby?
easy
A. "Hello nWorld"
B. 'Hello\nWorld'
C. "Hello\\nWorld"
D. "Hello\nWorld"
Solution
Step 1: Understand how Ruby handles escape sequences
Escape sequences like \n (newline) work only inside double-quoted strings, not single-quoted.
Step 2: Identify the correct string with newline
Double quotes with \n will create a newline, so "Hello\nWorld" is correct.
Final Answer:
"Hello\nWorld" -> Option D
Quick Check:
Escape sequences need double quotes [OK]
Hint: Use double quotes for special characters like \n [OK]
Common Mistakes:
Using single quotes which treat \n as literal text
Forgetting to escape backslash properly
Confusing single and double quotes for escape sequences
3. What will be the output of the following Ruby code?
name = "Alice"
puts 'Hello, #{name}!'
puts "Hello, #{name}!"
medium
A. Hello, #{name}!
Hello, Alice!
B. Hello, Alice!
Hello, Alice!
C. Hello, #{name}!
Hello, #{name}!
D. Hello, Alice!
Hello, #{name}!
Solution
Step 1: Understand string interpolation in Ruby
Interpolation #{variable} works only inside double-quoted strings, not single-quoted.
Step 2: Analyze each puts statement output
First puts uses single quotes, so prints literally 'Hello, #{name}!'. Second uses double quotes, so inserts variable value 'Alice'.
Final Answer:
Hello, #{name}!
Hello, Alice! -> Option A
Quick Check:
Interpolation only in double quotes [OK]
Hint: Only double quotes allow #{variable} interpolation [OK]
Common Mistakes:
Assuming interpolation works in single quotes
Confusing output order
Ignoring difference between single and double quotes
4. Identify the error in the following Ruby code:
str = 'It's a sunny day'
medium
A. SyntaxError due to missing double quotes
B. No error, string is valid
C. SyntaxError due to unescaped single quote inside single-quoted string
D. Runtime error due to invalid string
Solution
Step 1: Check string delimiters and content
The string uses single quotes outside and contains an unescaped single quote inside (It's), which breaks syntax.
Step 2: Understand how to fix the error
To fix, either escape the inner single quote as \' or use double quotes outside.
Final Answer:
SyntaxError due to unescaped single quote inside single-quoted string -> Option C
Quick Check:
Escape inner quotes or use double quotes [OK]
Hint: Escape inner quotes or use double quotes [OK]
Common Mistakes:
Ignoring the need to escape inner single quotes
Assuming single quotes allow unescaped inner single quotes
Confusing syntax and runtime errors
5. You want to create a string that contains both single and double quotes exactly as: She said, "It's fine." Which of the following Ruby strings correctly represents this?
hard
A. 'She said, 'It\'s fine.''
B. "She said, \"It's fine.\""
C. "She said, "It's fine.""
D. 'She said, "It\'s fine."'
Solution
Step 1: Analyze the string content
The string contains double quotes around the phrase and a single quote inside the word It's.
Step 2: Check each option for correct escaping
"She said, \"It's fine.\"" uses double quotes outside, escapes inner double quotes with \", and leaves single quote unescaped (allowed inside double quotes). This is correct.
Step 3: Identify errors in other options
'She said, "It\'s fine."' uses single quotes outside but escapes single quote inside, which is valid but escapes double quotes unnecessarily. "She said, "It's fine."" has unescaped double quotes inside double quotes causing syntax error. 'She said, 'It\'s fine.'' has unescaped single quotes inside single quotes causing syntax error.