ruby-coder
This skill guides writing of new Ruby code following modern Ruby 3.x syntax, Sandi Metz's 4 Rules for Developers, and idiomatic Ruby best practices. Use when creating new Ruby files, writing Ruby methods, or refactoring Ruby code to ensure adherence to clarity, simplicity, and maintainability standards.
What this skill does
# Ruby Coder
## Ruby 3.x Modern Syntax
Use hash shorthand when keys match variable names:
```ruby
age = 49
name = "David"
user = { name:, age: }
```
For general naming conventions, semantic methods, and enumerable patterns, see `references/ruby-style-conventions.md`.
## Sandi Metz's 4 Rules for Developers
These rules enforce strict limits to maintain code quality. Breaking them requires explicit justification.
### Rule 1: Classes Can Be No Longer Than 100 Lines
**Limit**: Maximum 100 lines of code per class
**Check**: When a class exceeds this limit, extract secondary concerns to new classes
```ruby
# When exceeding 100 lines, extract secondary concerns:
class UserProfilePresenter # Presentation logic
class UserProfileValidator # Validation logic
class UserProfileNotifier # Notification logic
```
**Exceptions**: Valid Single Responsibility Principle (SRP) justification required
### Rule 2: Methods Can Be No Longer Than 5 Lines
**Limit**: Maximum 5 lines per method. Each if/else branch counts as lines.
```ruby
# Good - 5 lines or fewer
def process_order
validate_order
calculate_totals
apply_discounts
finalize_payment
end
# Avoid - extract when too long
def process_order
return unless items.any?
return unless valid_address?
self.subtotal = items.sum(&:price)
self.tax = subtotal * tax_rate
self.total = subtotal + tax
charge_customer
send_confirmation
end
```
**Exceptions**: Pair approval required (Rule 0: break rules with agreement)
### Rule 3: Pass No More Than 4 Parameters
**Limit**: Maximum 4 parameters per method. Use parameter objects or hashes when more data is needed.
```ruby
# Parameter object when data is related
def create_post(post_params)
Post.create(user: post_params.user, title: post_params.title, content: post_params.content)
end
```
**Exceptions**: Rails view helpers like `link_to` or `form_for` are exempt
### Rule 4: Controllers May Instantiate Only One Object
**Limit**: Controllers should instantiate only one object; other objects come through that via facade pattern.
```ruby
# Good - single object via facade
class DashboardController < ApplicationController
def show
@dashboard = DashboardFacade.new(current_user)
end
end
# Avoid - multiple instance variables
class DashboardController < ApplicationController
def show
@user = current_user
@posts = @user.posts.recent
@notifications = @user.notifications.unread
end
end
```
- Prefix unused instance variables with underscore: `@_calculation`
- Avoid direct collaborator access in views: `@user.profile.avatar` -> use facade method
## Code Quality Standards
### Thread Safety
Use `Mutex` for shared mutable state:
```ruby
class Configuration
@instance_mutex = Mutex.new
def self.instance
return @instance if @instance
@instance_mutex.synchronize { @instance ||= new }
end
end
```
### Idempotent Operations
Design operations to be safely repeatable:
```ruby
def activate_user
return if user.active?
user.update(active: true)
send_activation_email unless email_sent?
end
```
## Refactoring Triggers
Extract classes when:
- Class exceeds 100 lines (Sandi Metz Rule 1)
- Class has multiple responsibilities
- Class name contains "And" or "Or"
Extract methods when:
- Method exceeds 5 lines (Sandi Metz Rule 2)
- Conditional logic is complex
- Code has nested loops or conditionals
- Comments explain what code does (code should be self-explanatory)
Use parameter objects when:
- Methods require more than 4 parameters (Sandi Metz Rule 3)
- Related parameters are always passed together
- Parameter list is growing over time
Create facades when:
- Controllers need multiple objects (Sandi Metz Rule 4)
- Views access nested collaborators
- Complex data aggregation is needed
## When to Break Rules
Sandi Metz's "Rule 0": Break any of the 4 rules only with pair approval or clear justification.
| Rule | Valid Exception |
|------|---------------|
| 100 lines | Clear SRP justification required |
| 5 lines | Complex but irreducible algorithms |
| 4 params | Rails view helpers exempt |
| 1 object | Simple views without facades |
Document all exceptions with clear reasoning in code comments.
## References
- `references/sandi-metz.md` - Code smells, refactoring, testing principles
- `references/ruby-tips.md` - Type conversion, hash patterns, proc composition, refinements
- `references/ruby-style-conventions.md` - Naming, semantic methods, enumerables, composition
Related in Data & Analytics
clawarr-suite
IncludedComprehensive management for self-hosted media stacks (Sonarr, Radarr, Lidarr, Readarr, Prowlarr, Bazarr, Overseerr, Plex, Tautulli, SABnzbd, Recyclarr, Unpackerr, Notifiarr, Maintainerr, Kometa, FlareSolverr). Deep library exploration, analytics, dashboard generation, content management, request handling, subtitle management, indexer control, download monitoring, quality profile sync, library cleanup automation, notification routing, collection/overlay management, and media tracker integration (Trakt, Letterboxd, Simkl).
querying-soql
IncludedSOQL query generation, optimization, and analysis with 100-point scoring. Use this skill when the user needs SOQL/SOSL authoring or optimization: natural-language-to-query generation, relationship queries, aggregates, query-plan analysis, and performance or safety improvements for Salesforce queries. TRIGGER when: user writes, optimizes, or debugs SOQL/SOSL queries, touches .soql files, or asks about relationship queries, aggregates, or query performance. DO NOT TRIGGER when: bulk data operations (use handling-sf-data), Apex DML logic (use generating-apex), or report/dashboard queries.
app-store-optimization
IncludedApp Store Optimization (ASO) toolkit for researching keywords, analyzing competitor rankings, generating metadata suggestions, and improving app visibility on Apple App Store and Google Play Store. Use when the user asks about ASO, app store rankings, app metadata, app titles and descriptions, app store listings, app visibility, or mobile app marketing on iOS or Android. Supports keyword research and scoring, competitor keyword analysis, metadata optimization, A/B test planning, launch checklists, and tracking ranking changes.
habit-flow
IncludedAI-powered atomic habit tracker with natural language logging, streak tracking, smart reminders, and coaching. Use for creating habits, logging completions naturally ("I meditated today"), viewing progress, and getting personalized coaching.
app-store-optimization
IncludedApp Store Optimization (ASO) toolkit for researching keywords, analyzing competitor rankings, generating metadata suggestions, and improving app visibility on Apple App Store and Google Play Store. Use when the user asks about ASO, app store rankings, app metadata, app titles and descriptions, app store listings, app visibility, or mobile app marketing on iOS or Android. Supports keyword research and scoring, competitor keyword analysis, metadata optimization, A/B test planning, launch checklists, and tracking ranking changes.
visualizing-data
IncludedBuilds dashboards, reports, and data-driven interfaces requiring charts, graphs, or visual analytics. Provides systematic framework for selecting appropriate visualizations based on data characteristics and analytical purpose. Includes 24+ visualization types organized by purpose (trends, comparisons, distributions, relationships, flows, hierarchies, geospatial), accessibility patterns (WCAG 2.1 AA compliance), colorblind-safe palettes, and performance optimization strategies. Use when creating visualizations, choosing chart types, displaying data graphically, or designing data interfaces.