High
Ruby
Prefer composition over inheritance
Official
Rule Description
In Ruby, favor composition and modules over deep inheritance hierarchies:
- Use modules for shared behavior (concerns in Rails)
- Inject dependencies rather than inheriting from base classes
- Keep inheritance depth to 2-3 levels maximum
- Use duck typing - if it quacks like a duck, treat it like a duck
```ruby
# Good: Composition
class OrderProcessor
def initialize(payment_gateway:, notifier:)
@payment_gateway = payment_gateway
@notifier = notifier
end
end
# Avoid: Deep inheritance
class SpecialOrderProcessor < OrderProcessor < BaseProcessor < ApplicationService
```
Add This Rule
Sign in to add this rule to your workspace
Sign in with GitHubDetails
- Severity
- High
- Category
- Ruby
- Used in
- 3 rulesets