What is the recommended indentation style in Ruby?
Use 2 spaces per indentation level. Avoid tabs to keep code consistent and readable.
Click to reveal answer
beginner
How should method names be formatted in Ruby?
Method names should be written in snake_case, using lowercase letters and underscores to separate words.
Click to reveal answer
intermediate
When should you use single quotes vs double quotes for strings in Ruby?
Use single quotes for simple strings without interpolation or special symbols. Use double quotes when you need string interpolation or special characters like \n.
Click to reveal answer
intermediate
What is the preferred way to write blocks in Ruby?
Use curly braces {} for single-line blocks and do...end for multi-line blocks to improve readability.
Click to reveal answer
beginner
How should you handle trailing whitespace and line length in Ruby code?
Avoid trailing whitespace at the end of lines. Keep lines shorter than 80 characters to maintain readability.
Click to reveal answer
What is the correct indentation size recommended in Ruby style guides?
A2 spaces
B4 spaces
C1 tab
DNo indentation
✗ Incorrect
Ruby style guides recommend using 2 spaces for indentation to keep code clean and consistent.
Which naming style is preferred for Ruby method names?
AcamelCase
BPascalCase
Csnake_case
Dkebab-case
✗ Incorrect
Ruby method names should use snake_case with lowercase letters and underscores.
When should you use double quotes for strings in Ruby?
AAlways
BOnly for single characters
CNever
DWhen the string contains interpolation or special characters
✗ Incorrect
Double quotes allow string interpolation and special characters like newline \n.
Which block syntax is preferred for multi-line blocks in Ruby?
Ado...end
Bbegin...end
C{}
Dif...end
✗ Incorrect
Use do...end for multi-line blocks to improve readability.
What is the recommended maximum line length in Ruby style guides?
A100 characters
B80 characters
C120 characters
DNo limit
✗ Incorrect
Keeping lines under 80 characters helps maintain code readability.
Explain the key Ruby style guide rules for indentation, method naming, and string quoting.
Think about how to keep code clean and easy to read.
You got /4 concepts.
Describe the preferred Ruby block syntax and line length recommendations.
Consider readability and clarity in your answer.
You got /3 concepts.
Practice
(1/5)
1. According to the Ruby style guide essentials, which naming convention should you use for method names?
easy
A. snake_case
B. CamelCase
C. kebab-case
D. PascalCase
Solution
Step 1: Understand method naming conventions
Ruby style guide recommends using snake_case for method names to improve readability.
Step 2: Compare options with style guide
CamelCase and PascalCase are used for classes and modules, not methods.
Final Answer:
snake_case -> Option A
Quick Check:
Method names use snake_case [OK]
Hint: Methods and variables use snake_case, classes use CamelCase [OK]
Common Mistakes:
Using CamelCase for method names
Using kebab-case which is invalid in Ruby
Confusing class naming with method naming
2. Which of the following is the correct way to indent Ruby code according to the style guide?
easy
A. 2 spaces
B. 4 spaces
C. 1 tab
D. No indentation
Solution
Step 1: Recall indentation rules
The Ruby style guide specifies using 2 spaces for indentation to keep code clean and consistent.
Step 2: Evaluate options
4 spaces and tabs are common in other languages but not recommended in Ruby style guide.
Final Answer:
2 spaces -> Option A
Quick Check:
Indent with 2 spaces [OK]
Hint: Always indent Ruby code with 2 spaces, never tabs [OK]
Common Mistakes:
Using tabs instead of spaces
Using 4 spaces like in Python or JavaScript
No indentation causing unreadable code
3. What will be the output of the following Ruby code?
class MyClass
def greet
name = 'Alice'
puts "Hello, #{name}!"
end
end
obj = MyClass.new
obj.greet
medium
A. SyntaxError
B. Hello, #{name}!
C. Hello, name!
D. Hello, Alice!
Solution
Step 1: Understand string interpolation
Double quotes allow #{name} to be replaced by the variable's value, which is 'Alice'.
Step 2: Trace method call and output
Calling greet prints "Hello, Alice!" to the console.