Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
166: RuboCop for Style Enforcement
When you first run RuboCop on a real-world project, the sheer volume of red text can be overwhelming. I've noticed many developers fall into the trap of thinking that RuboCop is a "correctness" tool—that if it flags a line of code, that code is somehow "wrong" or "broken" in a technical sense. They treat RuboCop like a compiler error: something that must be fixed because the code is invalid.
Let's look at a quick example. Imagine you wrote this method to handle a simple user greeting:
def greet_user(user)
if user.name
puts "Hello, #{user.name}!"
else
puts 'Hello, Guest!'
end
end
RuboCop might scream at you for mixing single quotes ('Hello, Guest!') and double quotes ("Hello, #{user.name}!"). If you think RuboCop is a correctness tool, you'll spend an hour Googling whether single quotes are faster than double quotes in Ruby 3.x to justify your choice. But here's the reality: the code works perfectly. The computer doesn't care. The "error" isn't a bug; it's a style inconsistency.
Stop treating the linter as a law book and start seeing it as a team agreement
The goal of RuboCop isn't to force you to write "perfect" Ruby; it's to ensure that the code looks like it was written by a single person, even if ten different engineers touched the file. When I'm reviewing a pull request, the last thing I want to do is spend my mental energy noticing that some methods use do...end and others use {...} for the same type of block. It's cognitive noise.
RuboCop enforces the "Ruby Style Guide," which is a widely accepted set of community norms. Following these norms makes your code instantly more readable to any other Rubyist in the world. However, the "law" is actually a suggestion. The real power comes from realizing that RuboCop is an automated way to end arguments about tabs versus spaces or trailing commas so you can focus on the actual logic of your application.
Moving from "Fighting the Linter" to configuring your .rubocop.yml
I've seen juniors get frustrated and simply disable the linter because it's "too annoying." That's a mistake. Instead, you should be managing your .rubocop.yml file. This file is where you tell RuboCop, "I know the community likes X, but in this project, we prefer Y."
For example, if you find the default line length of 80 characters too restrictive for your modern widescreen monitor, you don't just ignore the warning. You explicitly change the configuration:
Layout/LineLength:
Max: 120
By doing this, you've turned a "nuisance" into a project standard. I actually prefer a slightly longer line length because it prevents unnecessary wrapping that can make complex hashes harder to read. The key is to be intentional. If a rule genuinely hinders your productivity or makes the code less readable, change the rule. Just make sure the whole team is using the same config file.
One pro tip: use rubocop -A (the "safe auto-correct") to handle the boring stuff like indentation and quotes. Don't waste your human brainpower fixing spacing; let the tool do it, and save your energy for the Metrics/AbcSize warnings—those are the ones telling you that your method is actually too complex and needs a refactor.
📋 Practical Task
Refactoring the Legacy Order Validator
You've just inherited a OrderValidator class that works perfectly but is a stylistic nightmare. It violates several community standards and team preferences, making it hard to maintain.
Your Goal: Use RuboCop (or manually apply the style guide) to clean up the following code. You must:
- Standardize all strings to use double quotes.
- Fix the inconsistent indentation in the
validate_itemsmethod. - Convert the
if/elseblock incheck_statusinto a ternary operator to reduce line count. - Remove the trailing whitespace on the empty lines.
class OrderValidator
def initialize(order)
@order = order
end
def check_status
if @order.status == 'pending'
return 'Waiting'
else
return 'Processed'
end
end
def validate_items
@order.items.each do |item|
if item.price < 0
puts 'Invalid price detected'
end
end
end
end
Once you have refactored the code, explain why using a .rubocop.yml file would be better than manually checking these style points during a code review.
There are no comments for now.