๐ฏ Why RuboCop HK?
# โ Before: Inconsistent code style across your team
class UserController
def show
@user = User.find(params[:id])
render json: {
name: @user.name,
email: @user.email
}
end
end
# โ
After: Beautiful, consistent code with RuboCop HK
class UsersController < ApplicationController
def show
@user = User.find_by(id: params[:id])
return render json: { error: "User not found" }, status: :not_found unless @user
render json: {
name: @user.name,
email: @user.email,
}
end
end
๐ Table of Contents
- ๐ Quick Start
- ๐ฏ Features
- ๐ฆ Installation & Setup
- โ๏ธ Configuration
- ๐ Rails 8 Compatibility
- ๐ Version Compatibility Matrix
- ๐ Usage Documentation
- ๐ ๏ธ Advanced Customization
- ๐๏ธ Migration Guide
- ๐ฏ Real-World Examples
- ๐จ Modern Rails Conventions
- ๐ง Editor Integration
- ๐ณ Docker & Containerization
- ๐ CI/CD Integration
- โก Performance Optimization
- โ FAQ & Troubleshooting
๐ Quick Start
โก 30-Second Setup
# 1. Add to your Gemfile
echo 'gem "rubocop-hk", "~> 1.1.0", require: false' >> Gemfile
# 2. Install the gem
bundle install
# 3. Create configuration
echo 'inherit_gem:\n rubocop-hk: config/default.yml' > .rubocop.yml
# 4. Run RuboCop
bundle exec rubocop
๐ฌ See it in action
``` $ bundle exec rubocop Inspecting 23 files ....................... 23 files inspected, no offenses detected โจ Your code is now beautifully formatted! โจ ```๐ For detailed setup instructions, see QUICK_START.md
๐ฏ Features
| ### ๐ฅ **Modern Ruby & Rails** - โ **Rails 6.0-8.0+ Ready** - โ **Ruby 3.1-3.3+ Optimized** - โ **Rails 8 Omakase Integration** - โ **Rails 8 Authentication Patterns** - โ **Double Quote Strings** - โ **300+ Curated Rules** | ### ๐ **Developer Experience** - โ **Zero Configuration** - โ **Auto-correction** - โ **Fast Performance** - โ **IDE Integration** | ### ๐๏ธ **Flexible & Extensible** - โ **Plugin Architecture** - โ **Selective Disabling** - โ **Gradual Adoption** - โ **Custom Cop Support** |
๐ฆ Installation & Setup
๐ Copy-Paste Installation
๐ New Rails Application
```bash # Step 1: Add to Gemfile cat << 'EOF' >> Gemfile # Code quality and style enforcement group :development, :test do gem "rubocop-hk", "~> 1.1.0", require: false end EOF # Step 2: Install dependencies bundle install # Step 3: Create RuboCop configuration cat << 'EOF' > .rubocop.yml # RuboCop HK Configuration inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.3 TargetRailsVersion: 7.0 NewCops: enable EOF # Step 4: Run initial check bundle exec rubocop --autocorrect-all ```๐ Existing Rails Application
```bash # Step 1: Add to existing Gemfile bundle add rubocop-hk --group development,test --require false # Step 2: Backup existing config (if any) [ -f .rubocop.yml ] && cp .rubocop.yml .rubocop.yml.backup # Step 3: Create new configuration cat << 'EOF' > .rubocop.yml inherit_gem: rubocop-hk: config/default.yml # Gradually enable rules for legacy codebases inherit_from: .rubocop_todo.yml AllCops: TargetRubyVersion: 3.0 # Adjust to your Ruby version (3.0+) TargetRailsVersion: 6.0 # Adjust to your Rails version (6.0+) EOF # Step 4: Generate TODO list for gradual adoption bundle exec rubocop --auto-gen-config # Step 5: Auto-fix what you can bundle exec rubocop --autocorrect-all --safe ```๐ Standalone Ruby Application
```bash # Step 1: Install gem gem install rubocop-hk -v "~> 1.0.0" # Step 2: Create configuration cat << 'EOF' > .rubocop.yml inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.0 # Adjust to your Ruby version EOF # Step 3: Run RuboCop rubocop --autocorrect-all ```๐ Manual Installation
Add this line to your application's Gemfile:
group :development, :test do
gem "rubocop-hk", "~> 1.1.0", require: false
end
Then execute:
$ bundle install
Or install it directly:
$ gem install rubocop-hk -v "~> 1.0.0"
โ๏ธ Configuration
๐ฏ Basic Configuration
Create .rubocop.yml in your project root:
# ๐ง Basic RuboCop HK Setup
inherit_gem:
rubocop-hk: config/default.yml
AllCops:
TargetRubyVersion: 3.3 # Your Ruby version
TargetRailsVersion: 7.0 # Your Rails version (if using Rails)
NewCops: enable # Enable new cops as they're added
๐๏ธ Advanced Configuration
# ๐ Advanced RuboCop HK Configuration
inherit_gem:
rubocop-hk: config/default.yml
AllCops:
TargetRubyVersion: 3.3
TargetRailsVersion: 7.0
NewCops: enable
# Exclude common directories
Exclude:
- "tmp/**/*"
- "log/**/*"
- "node_modules/**/*"
- "public/**/*"
- "vendor/**/*"
- "db/schema.rb"
- "db/migrate/*.rb"
# ๐จ Customize specific cops
Style/Documentation:
Enabled: false # Disable documentation requirement
Metrics/ClassLength:
Max: 150 # Allow longer classes
Layout/LineLength:
Max: 120 # Extend line length
# ๐งช Testing configurations
RSpec/ExampleLength:
Max: 20 # Allow longer test examples
# ๐ Performance settings
Performance/Casecmp:
Enabled: true # Enable performance optimizations
๐ For more configuration options, see CUSTOMIZATION.md
๐ Rails 8 Compatibility
๐ Rails 8.0+ Enhanced Support
RuboCop HK fully embraces Rails 8.0+ Omakase RuboCop configuration while extending it with production-tested rules for real-world applications.
๐ What's New in Rails 8 Integration
- โ **Omakase Configuration Compatibility** - Works seamlessly with Rails 8's built-in RuboCop settings - โ **Enhanced Authentication Patterns** - Support for new Rails 8 authentication generators - โ **Modern Asset Pipeline** - Updated rules for Propshaft and new asset handling - โ **Improved Performance** - Optimized for Rails 8's enhanced performance features - โ **Solid Foundation** - Full support for Solid Cache, Queue, and Cable - โ **Kamal Deployment** - Integrated rules for modern deployment workflows๐ Rails 8 Migration Benefits
# Rails 8 Authentication (New Pattern)
class SessionsController < ApplicationController
# โ
Rails 8 style - using new authentication patterns
def create
user = User.authenticate_by(email: params[:email], password: params[:password])
return redirect_to login_path, alert: "Invalid credentials" unless user
login(user)
redirect_to root_path, notice: "Welcome back!"
end
private
def login(user)
session[:user_id] = user.id
end
end
# Rails 8 Solid Foundation Integration
class CacheService
# โ
Modern Rails 8 caching patterns
def fetch_user_data(user_id)
Rails.cache.fetch("user:#{user_id}", expires_in: 1.hour) do
expensive_user_calculation(user_id)
end
end
end
# Rails 8 Deployment with Kamal
# config/deploy.yml - Now properly supported by our rules
๐ Version Compatibility & Support Matrix
๐ฏ Complete Ruby & Rails Compatibility
| Ruby Version | Rails Version | RuboCop HK | Status | Production Ready | Performance | Notes |
|---|---|---|---|---|---|---|
| ๐ข Ruby 3.3+ | Rails 8.0+ | v0.1.0+ | โ Fully Supported | โ Excellent | โก Fastest | Latest Rails 8 Omakase + Enhanced rules |
| ๐ข Ruby 3.3 | Rails 7.2 | v0.1.0+ | โ Fully Supported | โ Excellent | โก Fast | Recommended for new projects |
| ๐ข Ruby 3.3 | Rails 7.1 | v0.1.0+ | โ Fully Supported | โ Excellent | โก Fast | Stable and feature-rich |
| ๐ข Ruby 3.3 | Rails 7.0 | v0.1.0+ | โ Fully Supported | โ Excellent | โก Fast | Most stable combination |
| ๐ก Ruby 3.2 | Rails 8.0+ | v0.1.0+ | โ Supported | โ Very Good | ๐ Good | Excellent production choice |
| ๐ก Ruby 3.2 | Rails 7.0-7.2 | v0.1.0+ | โ Supported | โ Very Good | ๐ Good | Widely adopted in enterprise |
| ๐ Ruby 3.1 | Rails 7.0-8.0 | v0.1.0+ | โ Minimum Support | โ Good | ๐ Adequate | Consider upgrading Ruby soon |
| ๐ Ruby 3.1 | Rails 6.1 | v0.1.0+ | โ Legacy Support | โ Good | ๐ Adequate | Legacy applications only |
| ๐ Ruby 3.1 | Rails 6.0 | v0.1.0+ | โ ๏ธ Limited | โ ๏ธ Caution | ๐ Slow | Upgrade strongly recommended |
| ๐ด Ruby 3.0 | Rails 5.2-6.1 | v0.1.0+ | โ ๏ธ End of Life | โ Not Recommended | ๐ Poor | Security risk - upgrade immediately |
| ๐ด Ruby 2.7 | Rails 4.2-6.0 | โ Not Supported | โ Incompatible | โ Deprecated | โ N/A | End of life - unsupported |
๐ง RuboCop Dependencies Matrix
| Component | Version | Required For | Compatibility | Security | Notes |
|---|---|---|---|---|---|
| RuboCop Core | 1.79.2+ | All projects | โ Latest stable | ๐ Secure | Core functionality |
| rubocop-rails | 2.32.0+ | Rails projects | โ Rails 6.0-8.0+ | ๐ Secure | Rails-specific cops |
| rubocop-rspec | 3.6.0+ | RSpec testing | โ RSpec 3.0+ | ๐ Secure | Testing best practices |
| rubocop-performance | 1.25.0+ | All projects | โ Ruby 3.1+ | ๐ Secure | Performance optimizations |
| thor | 1.0+ | CLI functionality | โ Ruby 3.1+ | ๐ Secure | Command-line interface |
| Ruby | 3.1.0+ | All projects | โ Required minimum | ๐ Secure | Minimum supported version |
๐ฏ Framework & Library Compatibility
| #### โ **Fully Supported (Tier 1)** - ๐ **Ruby on Rails** 6.0 - 8.0+ - ๐งช **RSpec** 3.0+ - ๐ **Minitest** 5.0+ - ๐ **Capybara** 3.0+ - ๐ญ **Factory Bot** 6.0+ - ๐ฏ **Pundit** 2.0+ - ๐ก **GraphQL Ruby** 1.8+ - ๐จ **ViewComponent** 2.0+ - ๐ **Devise** 4.0+ - ๐ **Kaminari** 1.0+ | #### ๐ก **Partially Supported (Tier 2)** - ๐ ๏ธ **Sinatra** (Basic support) - ๐ง **Hanami** 2.0+ (Community rules) - ๐ช **Grape** 1.0+ (API-focused rules) - ๐๏ธ **Dry-rb** (Functional patterns) - ๐ **Trailblazer** (Operation patterns) - ๐ญ **ROM** (Data mapping) - ๐ **Sidekiq** 6.0+ (Job processing) - ๐ **ActiveJob** (Built into Rails) |
๐ Testing Matrix (CI/CD Verified)
๐ Available Configurations
๐ฏ Core Configuration Files
| File | Purpose | When to Use |
|---|---|---|
config/default.yml |
๐ฏ Main configuration | Standard Rails applications |
config/rubocop-rails.yml |
๐ Rails-specific rules | Rails applications only |
config/rubocop-rspec.yml |
๐งช RSpec testing rules | Projects using RSpec |
config/rubocop-style.yml |
๐จ Style preferences | Code formatting standards |
config/rubocop-layout.yml |
๐ Layout and spacing | Code structure rules |
config/rubocop-metrics.yml |
๐ Complexity metrics | Code quality enforcement |
config/rubocop-lint.yml |
๐ Error prevention | Bug detection and prevention |
config/rubocop-performance.yml |
โก Performance rules | Optimization recommendations |
๐ Configuration Inheritance Tree
๐ default.yml (Main Config)
โโโ ๐จ rubocop-style.yml โโ Double quotes, modern syntax
โโโ ๐ rubocop-rails.yml โโ Rails 7+ conventions
โโโ ๐งช rubocop-rspec.yml โโ Testing best practices
โโโ ๐ rubocop-layout.yml โโ Code structure & spacing
โโโ ๐ rubocop-metrics.yml โโ Complexity & maintainability
โโโ ๐ rubocop-lint.yml โโ Error detection & prevention
โโโ โก rubocop-performance.yml โโ Performance optimizations
๐ Usage Documentation
๐ฏ Installation for Different Project Types
๐ New Rails 8 Application
```bash # Generate new Rails 8 app with RuboCop HK pre-configured rails new my_app --css=tailwind --database=postgresql cd my_app # Add RuboCop HK to Gemfile cat >> Gemfile << 'EOF' # Code quality and linting group :development, :test do gem "rubocop-hk", "~> 1.1.0", require: false end EOF bundle install # Create optimized Rails 8 configuration cat > .rubocop.yml << 'EOF' # Rails 8 Omakase + RuboCop HK Enhanced Configuration inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.3 TargetRailsVersion: 8.0 NewCops: enable # Rails 8 optimizations Rails/ApplicationController: Enabled: true Rails/ApplicationRecord: Enabled: true Rails/SkipsModelValidations: AllowedMethods: - touch - increment! - decrement! - update_attribute # Rails 8 Authentication patterns Rails/Output: Exclude: - "app/controllers/sessions_controller.rb" EOF # Run initial auto-correction bundle exec rubocop --autocorrect-all # Optional: Set up pre-commit hook echo 'bundle exec rubocop --autocorrect' > .git/hooks/pre-commit chmod +x .git/hooks/pre-commit ```๐๏ธ Existing Legacy Rails Application
```bash # Backup existing configuration [ -f .rubocop.yml ] && cp .rubocop.yml .rubocop.yml.backup.$(date +%Y%m%d) # Add to existing Gemfile bundle add rubocop-hk --group development,test --require false # Create gradual adoption configuration cat > .rubocop.yml << 'EOF' # Gradual adoption for legacy Rails applications inherit_gem: rubocop-hk: config/default.yml # Import generated TODO file for gradual adoption inherit_from: .rubocop_todo.yml AllCops: TargetRubyVersion: 3.1 # Adjust to your Ruby version TargetRailsVersion: 6.0 # Adjust to your Rails version NewCops: disable # Start conservative for legacy apps # Legacy app exclusions (customize as needed) AllCops: Exclude: - "db/**/*" - "config/**/*" - "vendor/**/*" - "node_modules/**/*" - "bin/**/*" - "log/**/*" - "tmp/**/*" - "public/**/*" - "storage/**/*" # Disable problematic cops for legacy apps initially Metrics/ClassLength: Enabled: false Metrics/MethodLength: Enabled: false Style/Documentation: Enabled: false EOF # Generate TODO file for gradual adoption bundle exec rubocop --auto-gen-config # Auto-fix safe issues only bundle exec rubocop --autocorrect --safe echo "โ Setup complete! Review .rubocop_todo.yml and gradually enable rules." ```๐ก Rails API-Only Application
```bash # API-only Rails app setup rails new my_api --api --database=postgresql cd my_api # Add RuboCop HK with API optimizations cat >> Gemfile << 'EOF' group :development, :test do gem "rubocop-hk", "~> 1.1.0", require: false end EOF bundle install # Create API-optimized configuration cat > .rubocop.yml << 'EOF' # API-only Rails application configuration inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.3 TargetRailsVersion: 8.0 # API-specific customizations Rails/ApplicationController: Enabled: true # Disable view-related cops for API-only apps Rails/OutputSafety: Enabled: false Rails/LinkToBlank: Enabled: false # API response patterns Style/HashSyntax: EnforcedStyle: ruby19_no_mixed_keys Layout/HashAlignment: EnforcedHashRocketStyle: key EnforcedColonStyle: key EOF bundle exec rubocop --autocorrect-all ```๐ Ruby Gem Development
```bash # Create new gem with RuboCop HK bundle gem my_gem cd my_gem # Add to gemspec cat >> my_gem.gemspec << 'EOF' spec.add_development_dependency "rubocop-hk", "~> 1.0.0" EOF bundle install # Create gem-focused configuration cat > .rubocop.yml << 'EOF' # Ruby gem development configuration inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.1 # Support broader Ruby versions for gems # Gem-specific settings Style/Documentation: Enabled: true # Important for gems Metrics/BlockLength: Exclude: - "spec/**/*" - "*.gemspec" Naming/FileName: Exclude: - "lib/**/*" # Allow gem naming conventions EOF bundle exec rubocop --autocorrect-all ```๐ง Configuration Examples for Different Versions
๐ Rails 8.0+ Configuration (Recommended)
**Best for**: New projects, modern Rails applications ```yaml # .rubocop.yml - Rails 8 Optimized inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.3 # Latest stable Ruby TargetRailsVersion: 8.0 # Rails 8 features NewCops: enable # Enable new cops as released # Rails 8 Omakase Integration Rails/ApplicationController: Enabled: true SuperClass: ApplicationController Rails/ApplicationRecord: Enabled: true SuperClass: ApplicationRecord # Rails 8 Authentication patterns Rails/Output: Exclude: - "app/controllers/sessions_controller.rb" # Rails 8 auth patterns - "app/controllers/passwords_controller.rb" # Modern Rails 8 patterns Style/StringLiterals: EnforcedStyle: double_quotes # Rails 8 Omakase preference # Performance optimizations for Rails 8 Performance/StringReplacement: Enabled: true Performance/MapCompact: Enabled: true # Rails 8 Solid foundation Rails/CompactBlank: Enabled: true Rails/ResponseParsedBody: Enabled: true ``` **Installation**: ```bash rails new my_app --css=tailwind --database=postgresql cd my_app bundle add rubocop-hk --group development,test # Copy configuration above to .rubocop.yml bundle exec rubocop --autocorrect-all ```๐ฏ Rails 7.x Configuration (Stable)
**Best for**: Production applications, enterprise environments ```yaml # .rubocop.yml - Rails 7 Stable inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.2 # Stable Ruby for enterprise TargetRailsVersion: 7.1 # Rails 7.1 for stability NewCops: disable # Conservative for production # Rails 7 specific features Rails/CompactBlank: Enabled: true Rails/DuplicateAssociation: Enabled: true Rails/I18nLazyLookup: Enabled: true Rails/ResponseParsedBody: Enabled: true # Enterprise security settings Security/Open: Enabled: true Security/Eval: Enabled: true # Production-ready settings Rails/Output: Enabled: true Exclude: - "db/seeds.rb" - "lib/tasks/**/*" # Asset pipeline for Rails 7 Rails/FilePath: EnforcedStyle: arguments # Performance for Rails 7 apps Performance: Enabled: true ``` **Migration from Rails 6**: ```bash # Update Rails bundle update rails # Update configuration sed -i 's/TargetRailsVersion: 6/TargetRailsVersion: 7.1/' .rubocop.yml # Run safe auto-corrections bundle exec rubocop --autocorrect --safe ```๐๏ธ Rails 6.x Configuration (Legacy)
**Best for**: Legacy applications, gradual upgrades ```yaml # .rubocop.yml - Rails 6 Legacy Support inherit_gem: rubocop-hk: config/default.yml # Import legacy TODO file for gradual adoption inherit_from: .rubocop_todo.yml AllCops: TargetRubyVersion: 3.1 # Minimum supported TargetRailsVersion: 6.1 # Rails 6.1 for better support NewCops: disable # Avoid breaking changes # Disable newer Rails cops not available in Rails 6 Rails/CompactBlank: Enabled: false # Not available in Rails 6 Rails/ResponseParsedBody: Enabled: false # Rails 7+ feature Rails/I18nLazyLookup: Enabled: false # Rails 7+ feature # Legacy-friendly settings Metrics/ClassLength: Max: 200 # More lenient for legacy code Metrics/MethodLength: Max: 20 # Allow longer methods initially Style/Documentation: Enabled: false # Disable for legacy migration # Rails 6 specific exclusions AllCops: Exclude: - "db/**/*" - "config/environments/**/*" - "config/initializers/**/*" - "app/assets/**/*" - "vendor/**/*" - "node_modules/**/*" ``` **Gradual migration strategy**: ```bash # Generate TODO file bundle exec rubocop --auto-gen-config # Enable one department at a time # Week 1: Layout cops # Week 2: Style cops # Week 3: Rails cops # Week 4: Performance cops ```๐ Ruby-Only Configuration (Non-Rails)
**Best for**: Gems, libraries, non-Rails applications ```yaml # .rubocop.yml - Ruby-only projects inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.1 # Broad compatibility for gems # Disable Rails-specific cops Rails: Enabled: false # Disable RSpec cops if not using RSpec RSpec: Enabled: false # Gem-specific settings Style/Documentation: Enabled: true # Important for public gems Style/FrozenStringLiteralComment: Enabled: true # Performance for gems # Gem naming conventions Naming/FileName: Exclude: - "lib/**/*" # Allow gem naming patterns # Testing configuration Metrics/BlockLength: Exclude: - "spec/**/*" - "test/**/*" - "*.gemspec" # Security for public gems Security: Enabled: true ```๐ API-Only Rails Configuration
**Best for**: Microservices, JSON APIs, headless applications ```yaml # .rubocop.yml - API-only Rails inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.3 TargetRailsVersion: 8.0 # API-specific Rails cops Rails/ApplicationController: Enabled: true SuperClass: ActionController::API # Disable view-related cops Rails/OutputSafety: Enabled: false # No views in API-only Rails/LinkToBlank: Enabled: false # No HTML links Rails/ContentTag: Enabled: false # No HTML generation # JSON response patterns Style/HashSyntax: EnforcedStyle: ruby19_no_mixed_keys Style/StringHashKeys: Enabled: false # Allow string keys in responses # API testing patterns RSpec/ExampleLength: Max: 30 # API tests can be longer RSpec/MultipleExpectations: Max: 8 # Complex API responses # Performance for API responses Performance/StringReplacement: Enabled: true Performance/MapCompact: Enabled: true # Exclude non-API directories AllCops: Exclude: - "app/views/**/*" # No views - "app/helpers/**/*" # No view helpers - "app/assets/**/*" # No assets ```๐ง Configuration Examples for Different Team Sizes
๐ฅ Startup Team (2-5 developers)
**Focus: Speed, flexibility, learning-friendly** ```yaml # .rubocop.yml - Startup Configuration inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.3 TargetRailsVersion: 8.0 NewCops: enable # More lenient for small teams learning best practices Metrics/ClassLength: Max: 150 # Allow slightly longer classes while learning Metrics/MethodLength: Max: 15 # Slightly more flexible Style/Documentation: Enabled: false # Focus on code quality first # Encourage good practices but don't block development Layout/LineLength: Max: 120 # Allow more experimental patterns Style/ClassAndModuleChildren: Enabled: false # Startup-friendly RSpec settings RSpec/ExampleLength: Max: 25 # Allow longer examples for complex integration tests RSpec/MultipleExpectations: Max: 5 # Allow more expectations while learning TDD ```๐ข Enterprise Team (10+ developers)
**Focus: Consistency, maintainability, strict standards** ```yaml # .rubocop.yml - Enterprise Configuration inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.2 # Conservative for enterprise TargetRailsVersion: 7.0 NewCops: disable # Evaluate new cops before enabling # Strict metrics for large teams Metrics/ClassLength: Max: 100 Metrics/MethodLength: Max: 10 Metrics/CyclomaticComplexity: Max: 6 # Mandatory documentation for enterprise Style/Documentation: Enabled: true Exclude: - "spec/**/*" - "test/**/*" # Strict line length for readability Layout/LineLength: Max: 100 # Enterprise security settings Security/Open: Enabled: true # Mandatory error handling Style/RescueStandardError: EnforcedStyle: explicit # Enterprise RSpec standards RSpec/ExampleLength: Max: 15 RSpec/MultipleExpectations: Max: 3 RSpec/NestedGroups: Max: 3 ```๐ Open Source Project
**Focus: Community standards, broad compatibility** ```yaml # .rubocop.yml - Open Source Configuration inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.1 # Broad compatibility # Documentation is crucial for open source Style/Documentation: Enabled: true # Ensure broad Ruby version compatibility Style/FrozenStringLiteralComment: Enabled: true # Community-friendly settings Layout/LineLength: Max: 120 # Encourage descriptive naming Naming/VariableNumber: EnforcedStyle: snake_case # Open source security Security: Enabled: true # Community RSpec patterns RSpec/ExampleLength: Max: 20 # Allow detailed examples for documentation ```โก Advanced Customization Options
๐ฏ Selective Cop Enabling/Disabling
```yaml # Advanced selective configuration inherit_gem: rubocop-hk: config/default.yml # Enable specific cops with custom settings Style/TrailingCommaInArguments: EnforcedStyleForMultiline: comma Style/TrailingCommaInArrayLiteral: EnforcedStyleForMultiline: comma # Disable cops that conflict with team preferences Style/StringLiterals: Enabled: false # If you prefer single quotes # Customize specific file patterns Rails/Output: Exclude: - "app/controllers/admin/**/*" - "lib/debugging_helpers.rb" # Department-level configurations Lint: Enabled: true Style: Enabled: true Exclude: - "db/data_migrations/**/*" ```๐ ๏ธ Advanced Customization
๐ฏ Common Commands
# ๐ Check all files
bundle exec rubocop
# โก Auto-fix safe issues
bundle exec rubocop --autocorrect
# ๐ Auto-fix all issues (use with caution)
bundle exec rubocop --autocorrect-all
# ๐ Show progress and statistics
bundle exec rubocop --format progress
# ๐ Check specific files/directories
bundle exec rubocop app/ lib/
# ๐ฏ Check only specific cops
bundle exec rubocop --only Style/StringLiterals
# ๐ Generate configuration for legacy code
bundle exec rubocop --auto-gen-config
๐๏ธ Overriding RuboCop Rules
You can easily override or customize any RuboCop rule by adding it to your .rubocop.yml file:
# .rubocop.yml
inherit_gem:
rubocop-hk: config/default.yml
AllCops:
TargetRubyVersion: 3.3
TargetRailsVersion: 8.0
# ๐ซ Disable specific cops
Style/Documentation:
Enabled: false # Turn off documentation requirement
# ๐ข Adjust cop parameters
Metrics/ClassLength:
Max: 200 # Allow longer classes (default: 100)
Layout/LineLength:
Max: 120 # Extend line length (default: 80)
Exclude:
- "db/migrate/*.rb" # Exclude migrations from line length
# ๐ฏ Enable/disable cops for specific files
Style/FrozenStringLiteralComment:
Enabled: true
Exclude:
- "bin/*"
- "db/seeds.rb"
# ๐ Customize severity levels
Style/StringLiterals:
EnforcedStyle: double_quotes # Enforce double quotes
Severity: error # Make violations errors instead of warnings
# ๐งช RSpec-specific overrides
RSpec/ExampleLength:
Max: 25 # Allow longer test examples (default: 15)
RSpec/MultipleExpectations:
Max: 5 # Allow more expectations per test
# โก Performance cop customization
Performance/Casecmp:
Enabled: true
Severity: warning
# ๐ Rails-specific overrides
Rails/HasManyOrHasOneDependent:
Enabled: true
Severity: error # Make missing dependent option an error
๐ก Pro Tips for Overriding Rules:
- Start small: Override one rule at a time and test the impact
- Use Exclude: Prefer excluding specific files rather than disabling cops entirely
- Check severity: Use
warninginstead of disabling if you want to keep visibility - Document why: Add comments explaining why you're overriding specific rules
- Team agreement: Make sure your team agrees on rule changes
๐ Finding Rule Names:
# See all available cops
bundle exec rubocop --show-cops
# Find which cop is flagging a specific issue
bundle exec rubocop --format offenses
๐ง Pre-commit Hook Setup
# Install pre-commit gem
gem install pre-commit
# Initialize in your project
pre-commit install
# Create .pre-commit-config.yaml
cat << 'EOF' > .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: rubocop
name: RuboCop
entry: bundle exec rubocop --autocorrect
language: system
types: [ruby]
pass_filenames: false
EOF
๐๏ธ Migration & Upgrade Guide
๐ Rails Version Upgrades
๐ Rails 7 โ Rails 8 Migration
**Prerequisites**: - Ruby 3.1+ (Ruby 3.2+ recommended) - RuboCop HK 0.1.0+ - Clean RuboCop run on current Rails 7 setup **Step-by-Step Migration**: 1. **Preparation Phase** ```bash # Ensure clean state bundle exec rubocop bundle exec rspec # or your test suite # Backup current configuration cp .rubocop.yml .rubocop.yml.rails7.backup ``` 2. **Rails Upgrade** ```bash # Update Rails bundle update rails # Run Rails 8 upgrade tasks rails app:update ``` 3. **RuboCop Configuration Update** ```yaml # .rubocop.yml - Updated for Rails 8 inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.3 # Updated from 3.2 TargetRailsVersion: 8.0 # Updated from 7.x NewCops: enable # Enable Rails 8 cops # New Rails 8 specific cops Rails/CompactBlank: Enabled: true Rails/ResponseParsedBody: Enabled: true ``` 4. **Auto-correction and Testing** ```bash # Run auto-corrections bundle exec rubocop --autocorrect-all --safe # Test the application bundle exec rspec rails server # Manual testing ``` 5. **Rails 8 Feature Integration** ```ruby # Update authentication patterns # Before (Rails 7) user = User.find_by(email: params[:email])&.authenticate(params[:password]) # After (Rails 8) user = User.authenticate_by(email: params[:email], password: params[:password]) ``` **Common Issues & Solutions**: - โ ๏ธ **Authentication changes**: Update to Rails 8 `authenticate_by` pattern - โ ๏ธ **Asset pipeline changes**: Update asset references for Propshaft - โ ๏ธ **New cop violations**: Review and fix new Rails 8 specific rules๐ Rails 6 โ Rails 7 Migration
**Prerequisites**: - Ruby 3.0+ (Ruby 3.1+ recommended for best performance) - RuboCop HK 0.1.0+ - Update all gems to Rails 6.1 compatible versions first **Migration Strategy**: 1. **Pre-upgrade Preparation** ```bash # Update to Rails 6.1 first (if not already) bundle update rails # Fix any Rails 6 deprecations bundle exec rake rails:update # Clean RuboCop state bundle exec rubocop --autocorrect-all --safe ``` 2. **Dependency Updates** ```ruby # Gemfile updates for Rails 7 compatibility gem "rails", "~> 7.0.0" gem "rubocop-hk", "~> 1.1.0" # Update other gems gem "rspec-rails", "~> 6.0" # Rails 7 compatible gem "factory_bot_rails", "~> 6.2" ``` 3. **Configuration Updates** ```yaml # .rubocop.yml - Rails 7 configuration inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.1 TargetRailsVersion: 7.0 # Updated from 6.x NewCops: enable # New Rails 7 cops Rails/CompactBlank: Enabled: true Rails/DuplicateAssociation: Enabled: true Rails/I18nLazyLookup: Enabled: true ``` 4. **Code Pattern Updates** ```ruby # Rails 7 introduces new methods # Old Rails 6 pattern: users.reject(&:blank?) # New Rails 7 pattern (RuboCop HK will suggest): users.compact_blank ``` **Migration Timeline**: - **Week 1**: Dependencies and basic Rails upgrade - **Week 2**: RuboCop configuration update and auto-fixes - **Week 3**: Manual code review and Rails 7 feature adoption - **Week 4**: Testing and deployment๐ Ruby Version Upgrades
#### **Ruby 3.1 โ Ruby 3.2** ```bash # Update Ruby version rvm install 3.2.0 # or rbenv install 3.2.0 rvm use 3.2.0 # or rbenv local 3.2.0 # Update Gemfile sed -i 's/ruby "3.1"/ruby "3.2"/' Gemfile # Update RuboCop target sed -i 's/TargetRubyVersion: 3.1/TargetRubyVersion: 3.2/' .rubocop.yml # Bundle update bundle install bundle exec rubocop ``` #### **Ruby 3.2 โ Ruby 3.3** ```bash # Similar process with additional performance benefits rvm install 3.3.0 rvm use 3.3.0 # Update configuration for Ruby 3.3 features cat >> .rubocop.yml << 'EOF' # Ruby 3.3 performance optimizations Performance/StringReplacement: Enabled: true Performance/MapCompact: Enabled: true EOF ``` **Performance Comparison**: - Ruby 3.1 โ 3.2: ~10-15% performance improvement - Ruby 3.2 โ 3.3: ~15-20% performance improvement - RuboCop run time: ~25% faster on Ruby 3.3 vs 3.1๐ง RuboCop Version Migrations
#### **Updating RuboCop Dependencies** ```bash # Check current versions bundle exec rubocop --version gem list | grep rubocop # Update RuboCop HK (includes all dependencies) bundle update rubocop-hk # Or update individual components bundle update rubocop rubocop-rails rubocop-rspec rubocop-performance ``` #### **Handling New Cops** ```yaml # Gradual adoption of new cops AllCops: NewCops: disable # Start conservative # Enable specific new cops after evaluation Style/NewCopName: Enabled: true ``` #### **Managing Breaking Changes** ```bash # Generate new TODO file when major updates introduce issues bundle exec rubocop --auto-gen-config --force # Gradually reduce TODO file bundle exec rubocop --regenerate-todo ```๐ฏ Migration Troubleshooting
โ Common Migration Issues
#### **Issue: New Cops Breaking CI** ```bash # Problem: New RuboCop version introduces breaking cops # Solution: Disable new cops temporarily echo "AllCops: NewCops: disable" >> .rubocop.yml # Then enable them gradually ``` #### **Issue: Rails Version Conflicts** ```bash # Problem: RuboCop Rails cops not recognizing new Rails version # Solution: Update target Rails version sed -i 's/TargetRailsVersion: .*/TargetRailsVersion: 8.0/' .rubocop.yml ``` #### **Issue: Performance Degradation** ```bash # Problem: RuboCop running slower after upgrade # Solution: Clear cache and use parallel processing rm -rf tmp/rubocop_cache bundle exec rubocop --cache --parallel ``` #### **Issue: Conflicting Style Rules** ```bash # Problem: Team style conflicts with new defaults # Solution: Override specific rules echo "Style/StringLiterals: EnforcedStyle: single_quotes" >> .rubocop.yml ```๐ Migration Checklist
โ Pre-Migration Checklist
- [ ] **Backup current configuration** ```bash cp .rubocop.yml .rubocop.yml.backup.$(date +%Y%m%d) ``` - [ ] **Document current Ruby/Rails versions** ```bash ruby --version > migration_log.txt rails --version >> migration_log.txt bundle exec rubocop --version >> migration_log.txt ``` - [ ] **Clean current RuboCop state** ```bash bundle exec rubocop # Should pass ``` - [ ] **Full test suite passing** ```bash bundle exec rspec # or your test command ``` - [ ] **Dependencies audit** ```bash bundle audit bundle outdated ```โ Post-Migration Checklist
- [ ] **Verify RuboCop configuration loads** ```bash bundle exec rubocop --config .rubocop.yml --list-cops > /dev/null ``` - [ ] **Run comprehensive RuboCop check** ```bash bundle exec rubocop --format progress ``` - [ ] **Verify application starts** ```bash rails server --environment=development ``` - [ ] **Run full test suite** ```bash bundle exec rspec ``` - [ ] **Check CI/CD pipeline** - Ensure CI passes with new configuration - Update CI Ruby/Rails versions if needed - [ ] **Update documentation** - Update README.md with new versions - Update deployment guides - Notify team of changes๐๏ธ Legacy Migration Guide
๐ Migrating from Other RuboCop Configurations
๐ฆ From Standard RuboCop
**Before:** ```yaml # Old .rubocop.yml inherit_from: .rubocop_todo.yml AllCops: TargetRubyVersion: 2.7 Exclude: - 'db/migrate/*.rb' Style/StringLiterals: EnforcedStyle: single_quotes ``` **After:** ```yaml # New .rubocop.yml with RuboCop HK inherit_gem: rubocop-hk: config/default.yml inherit_from: .rubocop_todo.yml AllCops: TargetRubyVersion: 3.3 TargetRailsVersion: 8.0 # RuboCop HK uses double quotes by default # Remove Style/StringLiterals override to adopt modern standards ``` **Migration Steps:** ```bash # 1. Backup current config cp .rubocop.yml .rubocop.yml.backup # 2. Install RuboCop HK bundle add rubocop-hk --group development,test # 3. Update configuration (see above) # 4. Generate new TODO for remaining issues bundle exec rubocop --auto-gen-config --force # 5. Auto-fix what's possible bundle exec rubocop --autocorrect-all --safe ```๐๏ธ From Shopify's RuboCop Config
**Key Differences & Migration:** ```yaml # Shopify config similarities (keep these) Layout/LineLength: Max: 120 # RuboCop HK enhancements Style/StringLiterals: EnforcedStyle: double_quotes # RuboCop HK standard Rails/ApplicationController: Enabled: true # Enhanced Rails rules # Migration command bundle remove rubocop-shopify bundle add rubocop-hk --group development,test ```โ๏ธ From Custom Configuration
**Step-by-Step Migration:** ```bash # 1. Audit current configuration bundle exec rubocop --list-cops > current_cops.txt # 2. Create hybrid configuration cat > .rubocop.yml << 'EOF' inherit_gem: rubocop-hk: config/default.yml # Preserve your custom rules temporarily Style/MyCustomRule: Enabled: true # Override RuboCop HK rules as needed Layout/LineLength: Max: 100 # If you prefer shorter lines EOF # 3. Gradual adoption bundle exec rubocop --auto-gen-config bundle exec rubocop --autocorrect-all --safe # 4. Review and clean up overrides over time ```๐ฏ Step-by-Step Migration Process
- Assessment Phase (Week 1) ```bash # Analyze current violations bundle exec rubocop --format json > rubocop-baseline.json
# Count violations by department bundle exec rubocop --format offenses
2. **Setup Phase** (Week 2)
```bash
# Install RuboCop HK
bundle add rubocop-hk --group development,test
# Create migration-friendly config
cat > .rubocop.yml << 'EOF'
inherit_gem:
rubocop-hk: config/default.yml
inherit_from: .rubocop_todo.yml
AllCops:
NewCops: disable # Start conservative
EOF
# Generate TODO file
bundle exec rubocop --auto-gen-config
- Gradual Adoption (Weeks 3-8) ```bash # Week 3: Auto-fix safe issues bundle exec rubocop --autocorrect --safe
# Week 4: Enable Layout cops # Week 5: Enable Style cops # Week 6: Enable Rails cops # Week 7: Enable Lint cops # Week 8: Final cleanup
---
## ๐ฏ Real-World Examples
### ๐ **Startup Team Configuration (2-5 developers)**
**Scenario:** Early-stage startup moving fast, learning Rails best practices
```yaml
# .rubocop.yml - Startup-friendly configuration
inherit_gem:
rubocop-hk: config/default.yml
AllCops:
TargetRubyVersion: 3.3
TargetRailsVersion: 8.0
NewCops: enable
# Relaxed metrics for rapid development
Metrics/ClassLength:
Max: 150
Metrics/MethodLength:
Max: 15
Metrics/BlockLength:
Exclude:
- "spec/**/*"
- "config/**/*"
# Skip documentation requirement initially
Style/Documentation:
Enabled: false
# Allow longer lines for readability
Layout/LineLength:
Max: 120
# Startup-specific exclusions
AllCops:
Exclude:
- "db/data/**/*" # Data migration scripts
- "lib/tasks/**/*" # Rake tasks can be more flexible
- "config/environments/**/*" # Environment configs
Benefits for Startups:
- โ Learn best practices gradually
- โ Don't block rapid feature development
- โ Establish foundation for growth
- โ Easy to tighten rules as team matures
๐ข Enterprise Team Configuration (10+ developers)
Scenario: Large engineering team, strict code quality requirements
# .rubocop.yml - Enterprise configuration
inherit_gem:
rubocop-hk: config/default.yml
AllCops:
TargetRubyVersion: 3.2 # Conservative for enterprise
TargetRailsVersion: 7.0
NewCops: disable # Evaluate new cops carefully
# Strict metrics for maintainability
Metrics/ClassLength:
Max: 100
Metrics/MethodLength:
Max: 10
Metrics/CyclomaticComplexity:
Max: 6
Metrics/PerceivedComplexity:
Max: 7
# Mandatory documentation
Style/Documentation:
Enabled: true
Exclude:
- "spec/**/*"
- "test/**/*"
- "db/**/*"
# Strict line length
Layout/LineLength:
Max: 100
# Security-first approach
Security:
Enabled: true
Security/Eval:
Enabled: true
Security/Open:
Enabled: true
# Performance requirements
Performance:
Enabled: true
# Strict RSpec requirements
RSpec/ExampleLength:
Max: 15
RSpec/MultipleExpectations:
Max: 3
RSpec/NestedGroups:
Max: 3
RSpec/DescribedClass:
Enabled: true
Benefits for Enterprise:
- โ Consistent code across large teams
- โ Reduced technical debt
- โ Enhanced security posture
- โ Improved maintainability
๐ Open Source Project Configuration
Scenario: Community-driven project with contributors worldwide
# .rubocop.yml - Open source configuration
inherit_gem:
rubocop-hk: config/default.yml
AllCops:
TargetRubyVersion: 3.1 # Broad compatibility
# Documentation is crucial for open source
Style/Documentation:
Enabled: true
Style/DocumentationMethod:
Enabled: true
# Frozen string literals for performance
Style/FrozenStringLiteralComment:
Enabled: true
# Comprehensive error handling
Style/RescueStandardError:
EnforcedStyle: explicit
# Security for public code
Security:
Enabled: true
# Community-friendly settings
Layout/LineLength:
Max: 120
# Encourage good naming
Naming/AccessorMethodName:
Enabled: true
Naming/PredicateName:
Enabled: true
# Comprehensive testing requirements
RSpec/ExampleLength:
Max: 20 # Allow detailed examples for documentation
RSpec/MultipleExpectations:
Max: 5 # Complex scenarios may need more assertions
๐ก Rails API-Only Application
Scenario: Microservice or API-only Rails application
# .rubocop.yml - API-focused configuration
inherit_gem:
rubocop-hk: config/default.yml
AllCops:
TargetRubyVersion: 3.3
TargetRailsVersion: 8.0
# API-specific Rails rules
Rails/ApplicationController:
Enabled: true
Rails/ApplicationRecord:
Enabled: true
# Disable view-related cops
Rails/OutputSafety:
Enabled: false
Rails/LinkToBlank:
Enabled: false
Rails/ContentTag:
Enabled: false
# API response formatting
Style/HashSyntax:
EnforcedStyle: ruby19_no_mixed_keys
Style/StringHashKeys:
Enabled: false # Allow string keys in API responses
# JSON serialization patterns
Layout/HashAlignment:
EnforcedHashRocketStyle: key
EnforcedColonStyle: key
# API testing patterns
RSpec/ExampleLength:
Max: 30 # API integration tests can be longer
# Exclude API documentation
AllCops:
Exclude:
- "app/views/**/*" # No views in API-only
- "app/helpers/**/*" # No view helpers
- "public/**/*"
๐ช Microservices Architecture
Scenario: Multiple small services with shared standards
# .rubocop.yml - Microservice template
inherit_gem:
rubocop-hk: config/default.yml
AllCops:
TargetRubyVersion: 3.3
TargetRailsVersion: 8.0
# Microservice-optimized metrics
Metrics/ClassLength:
Max: 80 # Keep services small
Metrics/MethodLength:
Max: 12 # Focused methods
# Essential for distributed systems
Style/Documentation:
Enabled: true
# Error handling is critical
Style/RescueStandardError:
EnforcedStyle: explicit
Lint/RescueException:
Enabled: true
# Performance matters in microservices
Performance:
Enabled: true
Performance/StringReplacement:
Enabled: true
# Container-friendly exclusions
AllCops:
Exclude:
- "docker/**/*"
- "k8s/**/*"
- "helm/**/*"
๐จ Modern Rails Conventions
๐ Rails 8 Patterns & Best Practices
๐ Modern Query Methods
# โ
Rails 6-8 preferred patterns
user = User.find_by(email: params[:email])
users = User.where(status: :active).limit(10)
recent_posts = Post.includes(:author).recent.published
# โ Legacy patterns to avoid
user = User.find_by_email(params[:email]) # Deprecated
users = User.find(:all, conditions: {status: "active"}) # Rails 2 style
๐ Rails 8 Authentication Patterns
# โ
Rails 8 authentication (new pattern)
class SessionsController < ApplicationController
def create
user = User.authenticate_by(email: params[:email], password: params[:password])
if user
login(user)
redirect_to dashboard_path, notice: "Welcome back!"
else
redirect_to login_path, alert: "Invalid credentials"
end
end
private
def login(user)
session[:user_id] = user.id
end
end
# โ
Rails 8 User model with authentication
class User < ApplicationRecord
has_secure_password
# New Rails 8 method
def self.authenticate_by(email:, password:)
find_by(email: email)&.authenticate(password)
end
end
๐๏ธ Modern Controller Patterns
# โ
Rails 8 controller best practices
class UsersController < ApplicationController
before_action :authenticate_user!
before_action :set_user, only: [:show, :edit, :update, :destroy]
def index
@users = User.includes(:profile)
.where(active: true)
.page(params[:page])
.per(20)
end
def create
@user = User.new(user_params)
if @user.save
redirect_to @user, notice: "User created successfully."
else
render :new, status: :unprocessable_entity
end
end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:name, :email, :role)
end
end
๐พ Rails 8 Caching with Solid Cache
# โ
Modern Rails 8 caching patterns
class ProductService
def self.featured_products
Rails.cache.fetch("featured_products", expires_in: 1.hour) do
Product.includes(:images).featured.limit(10).to_a
end
end
def self.user_recommendations(user)
cache_key = "recommendations/#{user.cache_key_with_version}"
Rails.cache.fetch(cache_key, expires_in: 30.minutes) do
RecommendationEngine.generate_for(user)
end
end
end
๐ API Development Patterns
# โ
Rails 8 API controller patterns
class Api::V1::UsersController < ApiController
before_action :authenticate_api_user!
def index
users = User.active.includes(:profile)
render json: {
data: users.map(&:api_representation),
meta: {
total: users.size,
page: params[:page] || 1,
},
}
end
def create
user = User.new(user_params)
if user.save
render json: { data: user.api_representation }, status: :created
else
render json: { errors: user.errors. }, status: :unprocessable_entity
end
end
private
def user_params
params.require(:user).permit(:name, :email)
end
end
๐งช Modern Testing Conventions
# โ
RSpec with Rails 8 patterns
RSpec.describe User, type: :model do
describe ".authenticate_by" do
let(:user) { create(:user, email: "user@example.com", password: "password") }
it "returns user with valid credentials" do
result = User.authenticate_by(email: "user@example.com", password: "password")
expect(result).to eq(user)
end
it "returns nil with invalid credentials" do
result = User.authenticate_by(email: "user@example.com", password: "wrong")
expect(result).to be_nil
end
end
end
# โ
Controller testing with Rails 8
RSpec.describe SessionsController, type: :controller do
describe "POST #create" do
let(:user) { create(:user, email: "test@example.com", password: "password") }
context "with valid credentials" do
it "logs in the user" do
post :create, params: { email: "test@example.com", password: "password" }
expect(session[:user_id]).to eq(user.id)
expect(response).to redirect_to(dashboard_path)
end
end
context "with invalid credentials" do
it "rejects the login" do
post :create, params: { email: "test@example.com", password: "wrong" }
expect(session[:user_id]).to be_nil
expect(response).to redirect_to(login_path)
end
end
end
end
โจ String Literals
# โ
Preferred - Double quotes everywhere for consistency
= "Hello, world!"
greeting = "Welcome, #{user.name}!"
multiline = "Line 1\nLine 2"
sql_query = "SELECT * FROM users WHERE active = true"
# โ Avoid - Single quotes (inconsistent with interpolation)
= 'Hello, world!' # Inconsistent
interpolation = 'Hello, ' + name # Inefficient concatenation
๐๏ธ Method Definitions
# โ
Preferred - Always use parentheses for clarity
def calculate_total(items, tax_rate: 0.0)
subtotal = items.sum(&:price)
subtotal * (1 + tax_rate)
end
def current_user_count
User.active.count
end
# โ
Use keyword arguments for better readability
def send_notification(user:, message:, urgent: false)
NotificationService.deliver(
recipient: user,
content: ,
priority: urgent ? :high : :normal,
)
end
# โ Avoid - Inconsistent parentheses and positional args
def process_order items, ={} # Hard to read and maintain
# ...
end
๐ฏ Trailing Commas
# โ
Preferred - Consistent trailing commas for better diffs
user_attributes = {
name: "John",
email: "john@example.com",
role: "admin", # <- Trailing comma
}
= [
"ruby",
"rails",
"programming", # <- Trailing comma
]
method_call(
first_param,
second_param,
third_param, # <- Trailing comma in method calls
)
# Benefits:
# โ
Cleaner git diffs when adding/removing items
# โ
Easier to reorder items
# โ
Consistent formatting across the codebase
# โ
Reduces merge conflicts
๐ Enhanced Security Practices
# โ
Rails 8 security best practices
class ApplicationController < ActionController::Base
# CSRF protection (enabled by default in Rails 8)
protect_from_forgery with: :exception
# Content Security Policy
content_security_policy do |policy|
policy.default_src :self
policy.script_src :self, :https
policy.style_src :self, :https, :unsafe_inline
end
private
# Strong parameters with explicit allowlisting
def user_params
params.require(:user).permit(
:name,
:email,
profile_attributes: [:bio, :avatar],
)
end
# Secure headers
def set_security_headers
response.headers["X-Frame-Options"] = "DENY"
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
end
end
# โ
Secure model validations
class User < ApplicationRecord
validates :email,
presence: true,
uniqueness: { case_sensitive: false },
format: { with: URI::MailTo::EMAIL_REGEXP }
validates :password,
length: { minimum: 8 },
confirmation: true,
if: :password_required?
# Sanitize user input
before_save :normalize_email
private
def normalize_email
self.email = email.downcase.strip if email.present?
end
end
๐ For complete style guide, see STYLEGUIDE.md
๐ง Editor Integration
๐ Visual Studio Code
๐ ๏ธ Setup Instructions
1. **Install Ruby extension:** ``` code --install-extension rebornix.Ruby ``` 2. **Configure settings** (`.vscode/settings.json`): ```json { "ruby.rubocop.executePath": "bundle exec", "ruby.rubocop.configFilePath": ".rubocop.yml", "ruby.format": "rubocop", "[ruby]": { "editor.formatOnSave": true, "editor.defaultFormatter": "rebornix.Ruby" }, "ruby.rubocop.autocorrect": true } ``` 3. **Keyboard shortcuts** (`.vscode/keybindings.json`): ```json [ { "key": "ctrl+alt+f", "command": "ruby.rubocop.autocorrect", "when": "editorLangId == ruby" } ] ```๐ Sublime Text
๐ ๏ธ Setup Instructions
1. **Install Package Control** and then install: - SublimeLinter - SublimeLinter-rubocop 2. **Configure** (`Preferences > Package Settings > SublimeLinter > Settings`): ```json { "linters": { "rubocop": { "executable": ["bundle", "exec", "rubocop"] } } } ```๐ฃ Vim/Neovim
๐ ๏ธ Setup Instructions
**Using ALE (Asynchronous Lint Engine):** ```vim " Install ALE plugin first Plug 'dense-analysis/ale' " Configure RuboCop let g:ale_ruby_rubocop_executable = 'bundle exec rubocop' let g:ale_ruby_rubocop_options = '--autocorrect' let g:ale_fixers = { \ 'ruby': ['rubocop'], \} let g:ale_fix_on_save = 1 ```๐ต IntelliJ IDEA / RubyMine
๐ ๏ธ Setup Instructions
1. **Go to:** `Settings > Tools > External Tools` 2. **Add new tool:** - Name: `RuboCop AutoCorrect` - Program: `bundle` - Arguments: `exec rubocop --autocorrect $FilePathRelativeToProjectRoot$` 3. **Add keyboard shortcut** in `Settings > Keymap`๐ณ Docker Support
๐ Quick Docker Setup
๐ฆ Dockerfile Example
```dockerfile FROM ruby:3.3-alpine # Install dependencies RUN apk add --no-cache \ build-base \ git \ linux-headers # Set working directory WORKDIR /app # Copy Gemfile COPY Gemfile Gemfile.lock ./ # Install gems RUN bundle install --jobs 4 --retry 3 # Copy application COPY . . # Run RuboCop CMD ["bundle", "exec", "rubocop"] ```๐ณ Docker Compose for CI
```yaml version: '3.8' services: rubocop: build: . volumes: - .:/app - bundle_cache:/usr/local/bundle command: bundle exec rubocop --format json --out rubocop-results.json volumes: bundle_cache: ```โก One-liner Docker Command
```bash # Run RuboCop in Docker without building an image docker run --rm -v $(pwd):/app -w /app ruby:3.3 \ bash -c "gem install rubocop-hk -v '~> 1.0.0' && rubocop --autocorrect" ```๐ CI/CD Integration
๐โโ๏ธ GitHub Actions
๐ง Complete Workflow
```yaml # .github/workflows/rubocop.yml name: ๐ง RuboCop Code Quality on: push: branches: [ main, develop ] pull_request: branches: [ main ] jobs: rubocop: name: ๐ RuboCop Analysis runs-on: ubuntu-latest steps: - name: ๐ฅ Checkout code uses: actions/checkout@v4 - name: ๐ Setup Ruby uses: ruby/setup-ruby@v1 with: ruby-version: 3.3 bundler-cache: true - name: ๐ Cache RuboCop uses: actions/cache@v3 with: path: ~/.cache/rubocop_cache key: $runner.os }-rubocop-$hashFiles('.rubocop.yml') } - name: ๐ Run RuboCop run: | bundle exec rubocop \ --format github \ --format json \ --out rubocop-results.json - name: ๐ Upload Results uses: actions/upload-artifact@v3 if: always() with: name: rubocop-results path: rubocop-results.json ```๐ฆ GitLab CI
๐ง Pipeline Configuration
```yaml # .gitlab-ci.yml stages: - quality ๐ง RuboCop: stage: quality image: ruby:3.3 cache: key: gems-cache paths: - vendor/ruby before_script: - bundle config set --local path vendor/ruby - bundle install --jobs $(nproc) script: - bundle exec rubocop --format junit --out rubocop-results.xml artifacts: reports: junit: rubocop-results.xml paths: - rubocop-results.xml expire_in: 1 week only: - merge_requests - main ```๐ Circle CI
๐ง Configuration
```yaml # .circleci/config.yml version: 2.1 executors: ruby-executor: docker: - image: cimg/ruby:3.3 working_directory: ~/project jobs: rubocop: executor: ruby-executor steps: - checkout - restore_cache: keys: - v1-dependencies-checksum "Gemfile.lock" } - run: name: ๐ฆ Install dependencies command: bundle install --jobs=4 --retry=3 --path vendor/bundle - save_cache: paths: - ./vendor/bundle key: v1-dependencies-checksum "Gemfile.lock" } - run: name: ๐ Run RuboCop command: | mkdir -p test-results/rubocop bundle exec rubocop --format RspecJunitFormatter \ --out test-results/rubocop/results.xml - store_test_results: path: test-results workflows: version: 2 test: jobs: - rubocop ```๐ฏ Jenkins
๐ง Pipeline Script
```groovy pipeline { agent any environment { BUNDLE_PATH = "$WORKSPACE/vendor/bundle" } stages { stage('๐ง Setup') { steps { sh 'bundle config set --local path $BUNDLE_PATH' sh 'bundle install --jobs 4' } } stage('๐ RuboCop Analysis') { steps { sh ''' bundle exec rubocop \ --format json \ --out rubocop-results.json \ --format junit \ --out rubocop-junit.xml ''' } post { always { archiveArtifacts artifacts: 'rubocop-*.json,rubocop-*.xml' publishTestResults testResultsPattern: 'rubocop-junit.xml' } } } } } ```๐ญ Production Version Recommendations
๐ Best Version Combinations for Production (2025)
#### **๐ฅ Tier 1: Recommended for New Projects** | Combination | Stability | Performance | Support | Use Case | |:------------|:----------|:-----------|:--------|:---------| | **Ruby 3.3 + Rails 8.0** | โญโญโญโญโญ | โกโกโกโกโก | ๐ก๏ธ 5+ years | New applications, greenfield projects | | **Ruby 3.2 + Rails 7.2** | โญโญโญโญโญ | โกโกโกโก | ๐ก๏ธ 4+ years | Enterprise applications, risk-averse teams | #### **๐ฅ Tier 2: Solid Production Choices** | Combination | Stability | Performance | Support | Use Case | |:------------|:----------|:-----------|:--------|:---------| | **Ruby 3.2 + Rails 8.0** | โญโญโญโญ | โกโกโกโกโก | ๐ก๏ธ 4+ years | Balanced approach, good performance | | **Ruby 3.3 + Rails 7.1** | โญโญโญโญ | โกโกโกโก | ๐ก๏ธ 3+ years | Latest Ruby with stable Rails | | **Ruby 3.1 + Rails 7.0** | โญโญโญโญ | โกโกโก | ๐ก๏ธ 2+ years | Conservative enterprise choice | #### **๐ฅ Tier 3: Maintenance Mode** | Combination | Stability | Performance | Support | Use Case | |:------------|:----------|:-----------|:--------|:---------| | **Ruby 3.1 + Rails 6.1** | โญโญโญ | โกโก | ๐ก๏ธ 1+ year | Legacy applications only | **Production Deployment Checklist**: ```bash # Before deploying to production bundle exec rubocop --fail-level error # Zero tolerance bundle exec rspec # Full test suite bundle audit # Security check bundle outdated # Dependency check ```โก Performance Benchmarks by Version
#### **RuboCop Execution Time (1000 files)** | Ruby Version | Rails Version | Execution Time | Memory Usage | Relative Performance | |:-------------|:--------------|:---------------|:-------------|:---------------------| | **Ruby 3.3** | Rails 8.0 | 12s | 180MB | ๐ **Baseline (100%)** | | **Ruby 3.2** | Rails 8.0 | 14s | 195MB | ๐ **85% performance** | | **Ruby 3.3** | Rails 7.2 | 13s | 175MB | ๐ **92% performance** | | **Ruby 3.2** | Rails 7.1 | 15s | 200MB | ๐ถ **80% performance** | | **Ruby 3.1** | Rails 7.0 | 18s | 220MB | ๐ **67% performance** | | **Ruby 3.1** | Rails 6.1 | 22s | 240MB | ๐ **55% performance** | **Performance Optimization Tips**: ```bash # Use parallel processing (2x speed improvement) bundle exec rubocop --parallel # Enable caching (3x speed improvement on subsequent runs) export RUBOCOP_OPTS="--cache --parallel" # For CI environments export RUBOCOP_CACHE_ROOT="tmp/rubocop_cache" ``` #### **Memory Usage Optimization** ```yaml # .rubocop.yml - Memory-optimized for large codebases AllCops: # Limit file processing for memory-constrained environments MaxFilesInCache: 5000 # Optimize exclusions to reduce memory footprint Exclude: - "**/*.log" - "tmp/**/*" - "node_modules/**/*" - "public/assets/**/*" - "vendor/bundle/**/*" ```๐ Security Considerations by Version
#### **Security Support Timeline** | Version | Security Support Until | Risk Level | Recommendation | |:--------|:----------------------|:-----------|:---------------| | **Ruby 3.3** | March 2027+ | ๐ข **Low** | โ Use in production | | **Ruby 3.2** | March 2026+ | ๐ข **Low** | โ Use in production | | **Ruby 3.1** | March 2025+ | ๐ก **Medium** | โ ๏ธ Plan upgrade | | **Ruby 3.0** | March 2024 | ๐ด **High** | โ Upgrade immediately | | **Rails 8.0** | 2029+ | ๐ข **Low** | โ Latest security features | | **Rails 7.1** | 2026+ | ๐ข **Low** | โ Active security support | | **Rails 7.0** | 2025+ | ๐ก **Medium** | โ Security patches only | | **Rails 6.1** | 2024+ | ๐ก **Medium** | โ ๏ธ Limited security support | **Security-Enhanced Configuration**: ```yaml # .rubocop.yml - Security-first configuration inherit_gem: rubocop-hk: config/default.yml # Enable all security cops Security: Enabled: true Security/Open: Enabled: true Security/Eval: Enabled: true Security/MarshalLoad: Enabled: true # Rails security patterns Rails/OutputSafety: Enabled: true Rails/LinkToBlank: Enabled: true ``` #### **Vulnerability Scanning Integration** ```bash # Add to your CI pipeline bundle audit --update # Check for vulnerabilities bundle exec rubocop --only Security # Run security cops only ```๐ Version-Specific Features & Limitations
๐ Rails 8 Exclusive Features
#### **New in Rails 8 (Supported by RuboCop HK)** - โ **Omakase RuboCop Integration** - Works alongside Rails 8's built-in config - โ **Enhanced Authentication** - `authenticate_by` method patterns - โ **Solid Cache Integration** - Modern caching patterns - โ **Propshaft Asset Pipeline** - New asset handling - โ **Kamal Deployment** - Container deployment patterns - โ **Enhanced Performance** - YJIT integration, better memory usage ```ruby # Rails 8 patterns fully supported by RuboCop HK class SessionsController < ApplicationController # New Rails 8 authentication pattern def create user = User.authenticate_by(email: params[:email], password: params[:password]) if user session[:user_id] = user.id redirect_to dashboard_path else redirect_to login_path, alert: "Invalid credentials" end end end # Solid Cache patterns class ProductService def self.trending Rails.cache.fetch("products:trending", expires_in: 1.hour) do Product.where("created_at > ?", 1.week.ago) .order(views_count: :desc) .limit(10) end end end ```โญ Rails 7 Specific Features
#### **Rails 7.0-7.2 Features (RuboCop HK Enhanced)** - โ **Zeitwerk Autoloader** - Enhanced file organization rules - โ **Hotwire Integration** - Turbo and Stimulus patterns - โ **Encrypted Attributes** - Security-focused rules - โ **ActiveStorage Variants** - Image processing patterns - โ **Parallel Testing** - CI/CD optimizations ```ruby # Rails 7 patterns with RuboCop HK validation class User < ApplicationRecord # Rails 7 encrypted attributes encrypts :ssn, deterministic: true encrypts :notes # Enhanced validation patterns validates :email, presence: true, uniqueness: { case_sensitive: false } end # Hotwire patterns class PostsController < ApplicationController def create @post = Post.new(post_params) if @post.save respond_to do |format| format.html { redirect_to @post } format.turbo_stream # Rails 7 Turbo integration end else render :new, status: :unprocessable_entity end end end ```๐๏ธ Rails 6 Legacy Support
#### **Rails 6.0-6.1 Limitations & Workarounds** - โ ๏ธ **Limited Modern Cops** - Some newer RuboCop rules not applicable - โ ๏ธ **Performance Differences** - Slower than Rails 7+ - โ ๏ธ **Security Patches Only** - Limited feature updates **Rails 6 Compatibility Configuration**: ```yaml # .rubocop.yml - Rails 6 specific inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRailsVersion: 6.1 # Maximum for Rails 6 NewCops: disable # Avoid incompatible cops # Disable Rails 7+ specific cops Rails/CompactBlank: Enabled: false # Not available in Rails 6 Rails/ResponseParsedBody: Enabled: false # Rails 7+ feature # Rails 6 specific allowances Rails/SkipsModelValidations: AllowedMethods: - update_attribute # Common in Rails 6 patterns ``` **Upgrade Path from Rails 6**: ```bash # Phase 1: Prepare (Week 1-2) bundle update --patch # Security updates only bundle exec rubocop --autocorrect-all --safe # Phase 2: Dependencies (Week 3) bundle update --minor # Minor version updates # Phase 3: Rails Upgrade (Week 4) bundle update rails # Full Rails upgrade # Update .rubocop.yml TargetRailsVersion # Phase 4: Modernization (Week 5+) # Adopt Rails 7/8 patterns gradually ```๐งช Testing Strategies by Version
๐ฌ Continuous Integration Testing Matrix
#### **Recommended CI Testing Strategy** ```yaml # .github/workflows/compatibility_matrix.yml name: Version Compatibility Matrix on: [push, pull_request] jobs: test: runs-on: ubuntu-latest strategy: fail-fast: false matrix: ruby: ['3.1', '3.2', '3.3'] rails: ['6.1', '7.0', '7.1', '8.0'] exclude: - ruby: '3.1' rails: '8.0' # Not recommended combination - ruby: '3.3' rails: '6.1' # Unnecessary combination steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: $matrix.ruby } bundler-cache: true - name: Create version-specific Gemfile run: | echo "source 'https://rubygems.org'" > Gemfile.test echo "gem 'rails', '~> $matrix.rails }'" >> Gemfile.test echo "gem 'rubocop-hk', path: '.'" >> Gemfile.test echo "gem 'rspec'" >> Gemfile.test - name: Run RuboCop with version matrix env: BUNDLE_GEMFILE: Gemfile.test run: | bundle install bundle exec rubocop --version bundle exec rubocop config/ lib/ --format json > results.json - name: Validate configuration compatibility run: | bundle exec ruby -e " require 'rubocop' config = RuboCop::ConfigLoader.load_file('config/default.yml') puts 'Configuration valid for Ruby $matrix.ruby }, Rails $matrix.rails }' " ```๐ Performance Testing Across Versions
#### **Benchmark Testing Setup** ```ruby # benchmark/version_performance.rb require 'benchmark' require 'rubocop' class VersionBenchmark def self.run puts "RuboCop HK Performance Benchmark" puts "Ruby: #RUBY_VERSION" puts "Rails: #Rails.version" if defined?(Rails) puts "RuboCop: #RuboCop::Version.version" puts "=" * 50 # Test different configurations configs = { "Basic" => "config/default.yml", "Rails" => "config/rubocop-rails.yml", "Performance" => "config/rubocop-performance.yml" } configs.each do |name, config_file| time = Benchmark.measure do system("bundle exec rubocop --config #config_file lib/ > /dev/null 2>&1") end puts "#name: #timetime.realtime.real.round(2)s (real), #timetime.utimetime.utime.round(2)s (user)" end end end VersionBenchmark.run ``` **Run benchmarks**: ```bash # Test current environment ruby benchmark/version_performance.rb # Test with different Ruby versions (using rbenv/rvm) for version in 3.1.4 3.2.2 3.3.0; do echo "Testing Ruby $version" rbenv local $version ruby benchmark/version_performance.rb done ```โ Version-Specific FAQ & Troubleshooting
๐จ Common Issues & Solutions
### **โ "Gem not found" Error** ```bash # Problem: Bundle can't find rubocop-hk LoadError: cannot load such file -- rubocop-hk # Solution: Ensure proper installation bundle install # Or for global installation: gem install rubocop-hk -v "~> 1.0.0" # Version-specific fix for Rails 6: echo 'gem "rubocop-hk", "~> 1.1.0", require: false' >> Gemfile bundle install ``` ### **โ Configuration Not Found** ```bash # Problem: RuboCop can't find configuration Configuration file not found: config/default.yml # Solution: Check your .rubocop.yml syntax inherit_gem: rubocop-hk: config/default.yml # Note: no quotes around config path # Verify gem installation bundle exec gem list | grep rubocop-hk ``` ### **โ Too Many Offenses** ```bash # Problem: Overwhelming number of violations in legacy code 1847 offenses detected # Solution: Generate TODO file for gradual adoption bundle exec rubocop --auto-gen-config echo "inherit_from: .rubocop_todo.yml" >> .rubocop.yml # For Rails 6 legacy projects: bundle exec rubocop --auto-gen-config --exclude-limit 1000 ``` ### **โ Performance Issues** ```bash # Problem: RuboCop running slowly # Solution: Enable caching and parallel processing bundle exec rubocop --cache --parallel # For older Ruby versions (3.1), use single-threaded mode: bundle exec rubocop --cache --no-parallel ``` ### **โ Rails Version Conflicts** ```bash # Problem: RuboCop cops not recognizing Rails features Rails/CompactBlank: undefined method `compact_blank' # Solution: Check Rails version compatibility grep -r "TargetRailsVersion" .rubocop.yml # Ensure it matches your actual Rails version # For Rails 6: Disable Rails 7+ cops echo "Rails/CompactBlank: Enabled: false" >> .rubocop.yml ``` ### **โ Ruby Version Incompatibility** ```bash # Problem: Cops requiring newer Ruby features SyntaxError: unexpected token # Solution: Update Ruby version in configuration sed -i 's/TargetRubyVersion: .*/TargetRubyVersion: 3.1/' .rubocop.yml # Or upgrade Ruby version: rbenv install 3.2.0 rbenv local 3.2.0 bundle install ``` ### **โ Memory Issues in CI** ```bash # Problem: RuboCop consuming too much memory in CI killed: memory limit exceeded # Solution: Optimize for CI environments export RUBY_GC_HEAP_GROWTH_FACTOR=1.1 export RUBY_GC_MALLOC_LIMIT=4000000 bundle exec rubocop --no-parallel # Disable parallel processing ```๐ค Frequently Asked Questions
### **Q: Can I use this with other RuboCop configs?** A: Yes! You can inherit multiple configurations: ```yaml inherit_gem: rubocop-hk: config/default.yml other-config: config/base.yml # Or combine with Rails 8 Omakase inherit_from: - .rubocop_rails_omakase.yml # Rails 8 built-in config inherit_gem: rubocop-hk: config/default.yml # RuboCop HK enhancements ``` ### **Q: How do I disable specific cops?** A: Add them to your `.rubocop.yml`: ```yaml Style/Documentation: Enabled: false # Version-specific disabling: Rails/CompactBlank: Enabled: false # Disable for Rails 6 projects ``` ### **Q: Can I use this in a non-Rails Ruby project?** A: Absolutely! Just use the base configuration: ```yaml inherit_gem: rubocop-hk: config/default.yml # Rails-specific cops will be automatically disabled Rails: Enabled: false # For gems, enable documentation Style/Documentation: Enabled: true ``` ### **Q: How do I contribute new rules?** A: Check our [Contributing Guide](CONTRIBUTING.md) for details on adding new cops or modifying existing ones. ### **Q: Which Ruby/Rails versions should I use for new projects?** A: **Recommended for 2025**: - **Ruby 3.3** + **Rails 8.0** - Best performance and latest features - **Ruby 3.2** + **Rails 7.2** - Enterprise-grade stability ### **Q: How do I upgrade from Rails 6 to Rails 7/8?** A: Follow our [Migration Guide](#๐๏ธ-migration--upgrade-guide) with step-by-step instructions for safe upgrades. ### **Q: Is RuboCop HK compatible with Rails 8 Omakase?** A: Yes! RuboCop HK works alongside Rails 8's built-in Omakase configuration. You can use both together for enhanced code quality. ### **Q: What if I'm stuck on an older Ruby/Rails version?** A: RuboCop HK provides legacy support: - **Ruby 3.1** + **Rails 6.1**: Basic support with limited features - **Ruby 3.0** and below: Not recommended for security reasons ### **Q: How do I handle version conflicts in my team?** A: Use our team-specific configurations: ```yaml # .rubocop.yml - Team consensus configuration inherit_gem: rubocop-hk: config/default.yml AllCops: TargetRubyVersion: 3.2 # Team's minimum Ruby version TargetRailsVersion: 7.0 # Team's Rails version NewCops: disable # Avoid surprise changes ```โก Performance Tips
```bash # ๐ Speed up RuboCop with these flags: bundle exec rubocop \ --cache \ # Enable caching --parallel \ # Run in parallel --format simple # Faster output format # ๐ For CI environments: bundle exec rubocop \ --cache false \ # Disable cache in CI --parallel \ # Still use parallel --format json # Machine-readable output ```โก Performance Optimization
๐ Speed Up RuboCop for Large Codebases
๐ฏ Essential Performance Settings
```yaml # .rubocop.yml - Performance optimized configuration inherit_gem: rubocop-hk: config/default.yml AllCops: # Enable caching for faster subsequent runs UseCache: true CacheRootDirectory: tmp MaxFilesInCache: 20000 # Parallel processing TargetRubyVersion: 3.3 TargetRailsVersion: 8.0 # Optimize file exclusions Exclude: - "tmp/**/*" - "log/**/*" - "node_modules/**/*" - "**/vendor/**/*" - "public/assets/**/*" - "public/packs/**/*" - "coverage/**/*" - ".git/**/*" ```โก Command-Line Optimizations
```bash # ๐ Fastest RuboCop execution bundle exec rubocop \ --cache \ --parallel \ --format simple # ๐ For CI environments (no cache, but parallel) bundle exec rubocop \ --cache false \ --parallel \ --format json \ --out rubocop-results.json # ๐ง Auto-correct with performance optimizations bundle exec rubocop \ --autocorrect \ --cache \ --parallel \ --format progress # ๐ Profile RuboCop performance time bundle exec rubocop --cache --parallel ```๐๏ธ Large Codebase Strategies
**1. Selective Analysis:** ```bash # Only check changed files in Git git diff --name-only --diff-filter=AM | grep '\.rb$' | xargs bundle exec rubocop # Only check specific directories bundle exec rubocop app/ lib/ --parallel # Only run specific cops bundle exec rubocop --only Style/StringLiterals,Layout/LineLength ``` **2. Incremental Adoption:** ```yaml # Focus on new code first inherit_gem: rubocop-hk: config/default.yml AllCops: NewCops: enable # Use TODO file for legacy code inherit_from: .rubocop_todo.yml # Custom exclusions for legacy parts Style/Documentation: Exclude: - "app/models/legacy/**/*" - "lib/legacy/**/*" ``` **3. Performance Monitoring:** ```bash # Measure performance improvements echo "Before optimization:" time bundle exec rubocop > /dev/null echo "After optimization:" time bundle exec rubocop --cache --parallel > /dev/null ```๐ฏ CI/CD Performance Tips
๐ GitHub Actions Optimization
```yaml # .github/workflows/rubocop.yml - Optimized name: RuboCop Analysis on: [push, pull_request] jobs: rubocop: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: 3.3 bundler-cache: true # Cache RuboCop analysis results - uses: actions/cache@v3 with: path: ~/.cache/rubocop_cache key: $runner.os }-rubocop-$hashFiles('.rubocop.yml') }-$hashFiles('Gemfile.lock') } restore-keys: | $runner.os }-rubocop-$hashFiles('.rubocop.yml') }- $runner.os }-rubocop- # Run RuboCop with performance optimizations - name: Run RuboCop run: | bundle exec rubocop \ --parallel \ --format github \ --format json --out rubocop-results.json - uses: actions/upload-artifact@v3 if: always() with: name: rubocop-results path: rubocop-results.json ```๐ง Memory Usage Optimization
๐พ Reduce Memory Footprint
```yaml # .rubocop.yml - Memory optimized inherit_gem: rubocop-hk: config/default.yml AllCops: # Limit parallel workers for memory-constrained environments # Default uses all CPU cores, but you can limit it TargetRubyVersion: 3.3 # For memory-constrained CI environments, disable parallel processing # Remove --parallel flag and run single-threaded # Disable memory-intensive cops for large codebases Metrics/ClassLength: Enabled: false # Disable for initial runs on large legacy codebases Metrics/MethodLength: Enabled: false # Can be memory intensive on huge methods ``` **Memory usage monitoring:** ```bash # Monitor memory usage during RuboCop run /usr/bin/time -v bundle exec rubocop 2>&1 | grep "Maximum resident set size" # For macOS /usr/bin/time -l bundle exec rubocop 2>&1 | grep "maximum resident set size" ```๐ Benchmarking and Metrics
๐ Performance Measurement
```bash # Create performance benchmark script cat > benchmark_rubocop.rb << 'EOF' require 'benchmark' puts "RuboCop Performance Benchmark" puts "=" * 50 # Test different configurations configs = { "Default (no flags)" => "", "With cache" => "--cache", "With parallel" => "--parallel", "Cache + Parallel" => "--cache --parallel", "Cache + Parallel + Simple format" => "--cache --parallel --format simple" } configs.each do |name, flags| puts "\n#name:" time = Benchmark.measure do `bundle exec rubocop #flags > /dev/null 2>&1` end puts " Real time: #timetime.realtime.real.round(2)s" puts " User time: #timetime.utimetime.utime.round(2)s" puts " System time: #timetime.stimetime.stime.round(2)s" end EOF ruby benchmark_rubocop.rb ```๐ฏ Version Testing Strategies
๐ฌ Local Development Testing
#### **Multi-Version Testing Setup** ```bash # Create version-specific gemfiles for testing # Gemfile.rails6 source 'https://rubygems.org' gem 'rails', '~> 6.1' gem 'rubocop-hk', path: '.' # Gemfile.rails7 source 'https://rubygems.org' gem 'rails', '~> 7.1' gem 'rubocop-hk', path: '.' # Gemfile.rails8 source 'https://rubygems.org' gem 'rails', '~> 8.0' gem 'rubocop-hk', path: '.' # Test script #!/bin/bash # test_versions.sh for rails in rails6 rails7 rails8; do echo "Testing with $rails" BUNDLE_GEMFILE="Gemfile.$rails" bundle install BUNDLE_GEMFILE="Gemfile.$rails" bundle exec rubocop done ``` #### **Ruby Version Management** ```bash # Using rbenv rbenv install 3.1.4 rbenv install 3.2.2 rbenv install 3.3.0 # Test across Ruby versions for version in 3.1.4 3.2.2 3.3.0; do rbenv local $version bundle install bundle exec rubocop done # Using Docker for isolation docker run --rm -v $(pwd):/app -w /app ruby:3.1 bundle exec rubocop docker run --rm -v $(pwd):/app -w /app ruby:3.2 bundle exec rubocop docker run --rm -v $(pwd):/app -w /app ruby:3.3 bundle exec rubocop ```๐๏ธ Production Deployment Testing
#### **Staging Environment Validation** ```bash # staging_validation.sh #!/bin/bash set -e echo "๐ Production Deployment Validation" echo "Ruby: $(ruby --version)" echo "Rails: $(rails --version)" echo "RuboCop HK: $(bundle list | grep rubocop-hk)" echo "===============================================" # 1. Configuration validation echo "๐ Validating RuboCop configuration..." bundle exec rubocop --config .rubocop.yml --list-cops > /dev/null echo "โ Configuration valid" # 2. Zero-tolerance RuboCop check echo "๐ Running RuboCop analysis..." bundle exec rubocop --fail-level error --format progress echo "โ No RuboCop violations" # 3. Security audit echo "๐ Security audit..." bundle audit --update echo "โ No known vulnerabilities" # 4. Performance check echo "โก Performance validation..." time bundle exec rubocop --cache --parallel > /dev/null echo "โ Performance acceptable" # 5. Memory usage check echo "๐พ Memory usage check..." /usr/bin/time -v bundle exec rubocop 2>&1 | grep "Maximum resident set size" echo "โ Memory usage within limits" echo "๐ All validation checks passed - Ready for production!" ``` #### **Blue-Green Deployment with RuboCop** ```yaml # .github/workflows/deploy.yml name: Blue-Green Deployment with Code Quality on: push: branches: [main] jobs: quality-gate: runs-on: ubuntu-latest outputs: quality-passed: $steps.rubocop.outcome == 'success' } steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: 3.3 bundler-cache: true - name: RuboCop Quality Gate id: rubocop run: | bundle exec rubocop --fail-level error --format github echo "RUBOCOP_VIOLATIONS=$(bundle exec rubocop --format json | jq '.summary.offense_count')" >> $GITHUB_OUTPUT - name: Block deployment on violations if: steps.rubocop.outputs.RUBOCOP_VIOLATIONS != '0' run: | echo "โ Deployment blocked: $steps.rubocop.outputs.RUBOCOP_VIOLATIONS } RuboCop violations" exit 1 deploy: needs: quality-gate if: needs.quality-gate.outputs.quality-passed == 'true' runs-on: ubuntu-latest steps: - name: Deploy to production run: echo "๐ Deploying clean code to production..." ```๐ Additional Resources
๐ Documentation
- ๐ Quick Start Guide - Get up and running in minutes
- ๐ Usage Examples - Comprehensive usage scenarios
- โ๏ธ Customization Guide - Advanced configuration options
- ๐จ Style Guide - Complete coding standards
- ๐๏ธ Architecture - Technical implementation details
๐ Useful Links
- RuboCop Documentation - Official RuboCop docs
- Ruby Style Guide - Community Ruby style guide
- Rails Coding Conventions - Rails team conventions
- Ruby Version Support - Official Ruby support timeline
- Rails Maintenance Policy - Rails version support policy
- RuboCop HK Compatibility Testing - CI/CD compatibility matrix
๐ค Community & Support
- Issues - Report bugs or request features
- Discussions - Ask questions and share ideas
- Wiki - Community-driven documentation
- Version Compatibility Reports - Version-specific issues and solutions
- Migration Help - Get help with version upgrades
๐ค Contributing
We โค๏ธ contributions! Whether it's:
- ๐ Bug reports
- ๐ก Feature requests
- ๐ Documentation improvements
- ๐ง Code contributions
- ๐ฌ Community discussions
๐ See our Contributing Guide to get started!
๐ Quick Development Setup
# 1. Fork & clone the repository
git clone https://github.com/hammadxcm/rubocop-hk.git
cd rubocop-hk
# 2. Install dependencies
bundle install
# 3. Run tests
bundle exec rspec
# 4. Check code quality
bundle exec rubocop
# 5. Make your changes and submit a PR! ๐
๐ License
This project is licensed under the MIT License - see the LICENSE.md file for details.