📖 Scenario: You are working on a small Ruby project where clean and readable code is important. Following a style guide helps everyone understand and maintain the code easily.
🎯 Goal: You will write Ruby code following essential style guide rules: using snake_case for variable names, proper indentation, and clear method definitions.
📋 What You'll Learn
Use snake_case for variable and method names
Indent code blocks with 2 spaces
Use meaningful variable names
Define a method with proper syntax
Print output using puts
💡 Why This Matters
🌍 Real World
Writing clean Ruby code helps teams work together and maintain projects easily.
💼 Career
Following style guides is a key skill for Ruby developers in professional environments.
Progress0 / 4 steps
1
Create a variable with snake_case
Create a variable called user_name and assign it the string "Alice".
Ruby
Hint
Use lowercase letters and underscores for variable names in Ruby.
2
Define a method with proper indentation
Define a method called greet_user that takes no arguments and prints "Hello, " followed by the user_name variable. Use 2 spaces for indentation inside the method.
Ruby
Hint
Indent the puts line with 2 spaces inside the method.
3
Call the method to print greeting
Call the method greet_user to display the greeting message.
Ruby
Hint
Simply write the method name followed by parentheses or without to call it.
4
Final output: Greeting message
Run the program and print the output of the greet_user method call.
Ruby
Hint
The program should print Hello, Alice exactly.
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.