# ๐Ÿ”ง RuboCop HK > **The Ultimate RuboCop Configuration for Modern Ruby & Rails 8.0+ Applications** [![Gem Version](https://badge.fury.io/rb/rubocop-hk.svg)](https://badge.fury.io/rb/rubocop-hk) [![Ruby](https://img.shields.io/badge/ruby-3.1+-red.svg)](https://www.ruby-lang.org) [![Rails](https://img.shields.io/badge/rails-6.0--8.0+-red.svg)](https://rubyonrails.org) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![RuboCop](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop) [![CI Status](https://img.shields.io/badge/CI-passing-brightgreen.svg)]() [![Downloads](https://img.shields.io/gem/dt/rubocop-hk.svg)](https://rubygems.org/gems/rubocop-hk) [![Maintainability](https://img.shields.io/badge/maintainability-A-brightgreen.svg)]() [![Test Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)]() [![Rails 8 Ready](https://img.shields.io/badge/Rails_8-Ready-brightgreen.svg)](https://rubyonrails.org) โœจ **Rails 8.0+ Compatible** | ๐Ÿš€ **Production-Ready** | ๐ŸŽฏ **Zero Configuration** | โšก **Performance Optimized** **[๐Ÿ“š Quick Start](#-quick-start) โ€ข [๐Ÿ“– Usage Guide](#-usage-documentation) โ€ข [โš™๏ธ Customization](#-advanced-customization) โ€ข [๐Ÿ—๏ธ Migration Guide](#-migration-guide) โ€ข [๐Ÿค Contributing](CONTRIBUTING.md)**

๐ŸŽฏ 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

โšก 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 , alert: "Invalid credentials" unless user

    (user)
    redirect_to root_path, notice: "Welcome back!"
  end

  private

  def (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

![Rails Compatibility](https://img.shields.io/badge/Rails-4.2--8.0+-brightgreen.svg) ![Ruby Compatibility](https://img.shields.io/badge/Ruby-3.1+-red.svg) ![RuboCop](https://img.shields.io/badge/RuboCop-1.79.2+-blue.svg) ![Production Ready](https://img.shields.io/badge/Production-Ready-brightgreen.svg)
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)

| Environment | Ruby 3.1 | Ruby 3.2 | Ruby 3.3 | Ruby 3.4-dev | |:------------|:--------:|:--------:|:--------:|:-------------:| | **Rails 8.0** | ๐ŸŸก | โœ… | โœ… | ๐Ÿ”ง | | **Rails 7.2** | โœ… | โœ… | โœ… | ๐Ÿ”ง | | **Rails 7.1** | โœ… | โœ… | โœ… | ๐Ÿ”ง | | **Rails 7.0** | โœ… | โœ… | โœ… | โš ๏ธ | | **Rails 6.1** | โœ… | โœ… | ๐ŸŸก | โŒ | | **Rails 6.0** | ๐ŸŸก | ๐ŸŸก | โš ๏ธ | โŒ | **Legend**: โœ… Fully Tested | ๐ŸŸก Basic Testing | ๐Ÿ”ง Development | โš ๏ธ Legacy | โŒ Not Supported

๐Ÿ“Š 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 warning instead 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

  1. 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
  1. 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
      (user)
      redirect_to dashboard_path, notice: "Welcome back!"
    else
      redirect_to , alert: "Invalid credentials"
    end
  end

  private

  def (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.full_messages }, 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()
      end
    end
  end
end

โœจ String Literals

# โœ… Preferred - Double quotes everywhere for consistency
message = "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)
message = '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: message,
    priority: urgent ? :high : :normal,
  )
end

# โŒ Avoid - Inconsistent parentheses and positional args
def process_order items, options={}  # 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
}

tags = [
  "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

๐Ÿค Community & Support


๐Ÿค 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.


## ๐ŸŒŸ Show Your Support If RuboCop HK helps you write better Ruby code, please consider: โญ **[Star this repo](https://github.com/hammadxcm/rubocop-hk)** โ€ข ๐Ÿฆ **[Share on Twitter](https://twitter.com/intent/tweet?text=Just%20found%20an%20awesome%20RuboCop%20config%20gem%21&url=https%3A//github.com/hammadxcm/rubocop-hk)** โ€ข ๐Ÿ’ฌ **[Spread the word](https://github.com/hammadxcm/rubocop-hk/discussions)** --- **Made with โค๏ธ by [Hammad Khan](https://github.com/hammadxcm)** **๐ŸŒ Visit: [https://fyniti.co.uk](https://fyniti.co.uk)** **[๐Ÿ› Report Bug](https://github.com/hammadxcm/rubocop-hk/issues) โ€ข [โœจ Request Feature](https://github.com/hammadxcm/rubocop-hk/issues/new?template=feature_request.md) โ€ข [๐Ÿ“– Documentation](https://github.com/hammadxcm/rubocop-hk/wiki) โ€ข [๐Ÿ’ฌ Get Support](https://github.com/hammadxcm/rubocop-hk/discussions)**