Ruby style guide essentials - Time & Space Complexity
When writing Ruby code, following a style guide helps keep code clean and easy to read.
We want to understand how the style choices affect the time it takes for code to run.
Analyze the time complexity of this Ruby method that formats and prints names.
def print_names(names)
names.each do |name|
formatted = name.strip.capitalize
puts formatted
end
end
This code goes through a list of names, cleans and formats each, then prints it.
Look for loops or repeated actions.
- Primary operation: Looping through each name in the list.
- How many times: Once for every name in the input list.
As the list of names gets bigger, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 formatting and printing steps |
| 100 | About 100 formatting and printing steps |
| 1000 | About 1000 formatting and printing steps |
Pattern observation: The work grows evenly as the list grows; double the names, double the work.
Time Complexity: O(n)
This means the time to finish grows directly with the number of names.
[X] Wrong: "Formatting each name is a fixed small cost, so time stays the same no matter how many names."
[OK] Correct: Each name needs its own formatting step, so more names mean more work.
Understanding how simple loops affect time helps you explain your code clearly and think about efficiency in real projects.
"What if we added a nested loop inside to compare each name with every other name? How would the time complexity change?"
Practice
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 AQuick Check:
Method names use snake_case [OK]
- Using CamelCase for method names
- Using kebab-case which is invalid in Ruby
- Confusing class naming with method naming
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 AQuick Check:
Indent with 2 spaces [OK]
- Using tabs instead of spaces
- Using 4 spaces like in Python or JavaScript
- No indentation causing unreadable code
class MyClass
def greet
name = 'Alice'
puts "Hello, #{name}!"
end
end
obj = MyClass.new
obj.greetSolution
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.Final Answer:
Hello, Alice! -> Option DQuick Check:
Double quotes enable interpolation = Hello, Alice! [OK]
- Using single quotes expecting interpolation
- Confusing variable name with string literal
- Expecting syntax error from interpolation
class my_class
def say_hello
puts 'Hello world'
end
endSolution
Step 1: Check class naming conventions
Ruby style guide requires class names to use CamelCase, not snake_case.Step 2: Verify method and string style
Method name is correctly snake_case; single quotes are allowed if no interpolation needed.Final Answer:
Class name should be CamelCase -> Option CQuick Check:
Classes use CamelCase [OK]
- Using snake_case for class names
- Changing method names to CamelCase incorrectly
- Forcing double quotes when not needed
def greetUser(name) return 'Hello, ' + name + '!' end
Solution
Step 1: Apply method naming conventions
Method names should use snake_case, so greet_user is correct.Step 2: Use string interpolation with double quotes
Double quotes allow cleaner string interpolation instead of concatenation.Final Answer:
def greet_user(name) "Hello, #{name}!" end -> Option BQuick Check:
snake_case + interpolation = def greet_user(name) "Hello, #{name}!" end [OK]
- Using CamelCase for method names
- Using single quotes with interpolation expecting it to work
- Concatenating strings instead of interpolation
Ready to prove it?
Pass the test and this pattern moves unknown
