---
title: "Ruby/Rails Modernization with Autohand Code"
description: "Upgrade legacy Ruby and Rails applications to modern versions. Transform Ruby 1.9/2.x codebases to Ruby 3+ with modern Rails patterns using Autohand Code."
sidebar:
  label: "Ruby Modernization"
seo:
  description: "Modernize Ruby and Rails applications with Autohand Code AI. Upgrade from Ruby 1.9/2.x to Ruby 3+, migrate legacy Rails versions, and modernize code architecture."
---

:::tip
**Ruby 3 Benefits:** Ruby 3 offers significant performance improvements and better concurrency support. Autohand Code helps teams modernize legacy Rails applications with minimal disruption. [Learn more](https://autohand.ai/contact-sales/).
:::

:::tip
**Related Skills:** Modernize your Ruby/Rails codebase with specialized skills from [Skilled](https://skilled.autohand.ai). Explore [Rails migration skills](https://skilled.autohand.ai/categories/frameworks), [Ruby-specific skills](https://skilled.autohand.ai/categories/languages), and [Ruby 3 upgrade workflows](https://skilled.autohand.ai/categories/workflows).
:::

## Why modernize Ruby/Rails? [#overview]

Modern Ruby and Rails offer compelling advantages:

- 3x performance improvement with Ruby 3
- Enhanced concurrency and parallelism support
- Modern Rails features and security improvements
- Better memory management and garbage collection
- Active community support and long-term maintenance

## Ruby/Rails codebase assessment [#assessment]

Assess your Ruby application for modernization readiness:

```bash
# Create an AGENTS.md to record your target (Ruby 3.2)
autohand init

# Generate an upgrade roadmap in read-only plan mode
autohand -p "Assess this Ruby 2.7 codebase for a move to Ruby 3.2. Write a roadmap to docs/ruby-roadmap.md" --plan

# Assessment includes:
# - Ruby version compatibility issues
# - Rails version compatibility
# - Gem dependency analysis
# - Deprecated API usage
```

## Ruby version upgrade [#ruby-upgrade]

Upgrade Ruby syntax and APIs to modern versions:

```bash
# Preview the upgrade as a patch
autohand -p "Upgrade lib/ and app/ from Ruby 2.7 to 3.2: fix keyword argument separation and use the fiber scheduler where it helps" --patch --output ruby32.patch
git apply --check ruby32.patch

# Execute the upgrade in an isolated git worktree
autohand --worktree ruby-3-2 -p "Upgrade lib/ and app/ from Ruby 2.7 to 3.2: fix keyword argument separation and use the fiber scheduler where it helps. Run bundle exec rspec when done"
```

### Example transformations

```ruby
# Before (Ruby 2.7)
def process_data(data, options = {})
  defaults = {timeout: 30, retries: 3}
  opts = defaults.merge(options)
  
  data.each do |item|
    puts "Processing: #{item}"
  end
end

# Hash with symbol keys
hash = { 'name' => 'John', 'age' => 30 }
```

Becomes:

```ruby
# After (Ruby 3.2)
def process_data(data, timeout: 30, retries: 3)
  data.each do |item|
    puts "Processing: #{item}"
  end
end

# Modern hash syntax
hash = { name: 'John', age: 30 }

# Pattern matching
case data
in { name:, age: } if age > 18
  puts "#{name} is an adult"
else
  puts "Invalid data"
end
```

## Rails framework modernization [#rails-upgrade]

Upgrade legacy Rails applications to modern versions:

```bash
# Plan the Rails 4.2 to 7.0 upgrade (read-only)
autohand -p "Plan an upgrade from Rails 4.2 to 7.0, including the Zeitwerk autoloader and encrypted credentials" --plan

# Run the upgrade one Rails version at a time in a worktree, checkpointing as it goes
autohand --worktree rails-7 --auto-mode "Upgrade Rails from 4.2 to 7.0 one minor version at a time, switch to Zeitwerk and encrypted credentials, and keep bundle exec rspec green" --max-iterations 40 --checkpoint-interval 5
```

### Controller transformation example

```ruby
# Before (Rails 4.x)
class UsersController < ApplicationController
  before_filter :authenticate_user!
  
  def index
    @users = User.all
    render json: @users
  end
  
  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to @user, notice: 'User was successfully created.'
    else
      render :new
    end
  end
  
  private
  
  def user_params
    params.require(:user).permit(:name, :email)
  end
end
```

Becomes:

```ruby
# After (Rails 7.x)
class UsersController < ApplicationController
  before_action :authenticate_user!
  
  def index
    @users = User.all
    render json: @users
  end
  
  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to @user, notice: 'User was successfully created.'
    else
      render :new, status: :unprocessable_entity
    end
  end
  
  private
  
  def user_params
    params.require(:user).permit(:name, :email)
  end
end
```

## Async programming modernization [#async-programming]

Introduce modern async patterns with fibers and concurrency:

```ruby
# Before (Synchronous)
def fetch_multiple_urls(urls)
  results = []
  urls.each do |url|
    response = HTTP.get(url)
    results << response.parse
  end
  results
end
```

Becomes:

```ruby
# After (Asynchronous with fibers)
require 'async'

def fetch_multiple_urls(urls)
  Async do
    tasks = urls.map do |url|
      Async do
        HTTP.get(url).parse
      end
    end
    tasks.map(&:wait)
  end
end
```

## Testing and validation [#testing]

Ensure modernization doesn't break functionality:

```bash
# Generate characterization tests for the legacy code before upgrading
autohand -p "Write RSpec characterization tests for app/models that pin their current behaviour"

# Validate the upgraded code against those tests
bundle exec rspec

# Run the suite on both Ruby versions (for example with rbenv)
RBENV_VERSION=2.7.8 bundle exec rspec && RBENV_VERSION=3.2.2 bundle exec rspec
```

### Modern RSpec example

```ruby
# Generated test (RSpec 3.x)
RSpec.describe UsersController, type: :controller do
  let(:user) { create(:user) }
  
  describe 'GET #index' do
    it 'returns a successful response' do
      get :index
      
      expect(response).to be_successful
    end
    
    it 'returns all users as JSON' do
      get :index
      
      expect(JSON.parse(response.body)).to include(
        'id' => user.id,
        'name' => user.name
      )
    end
  end
end
```

## Best practices for Ruby/Rails modernization [#best-practices]

### Pre-upgrade preparation

- **Comprehensive testing:** Achieve \>95% test coverage before starting
- **Gem audit:** Identify all gems and Ruby 3 compatibility
- **Performance baseline:** Document current performance metrics
- **Security review:** Address known security vulnerabilities
- **Team readiness:** Train team on Ruby 3 and modern Rails features

### Upgrade strategy

- **Incremental upgrades:** Upgrade Rails one major version at a time
- **Parallel development:** Maintain multiple Ruby versions during transition
- **Feature flags:** Use feature toggles for gradual rollout
- **Database migrations:** Plan and test database schema changes
- **API compatibility:** Maintain backward compatibility for external APIs

### Code transformation

- **Keyword arguments:** Adopt modern keyword argument syntax
- **Pattern matching:** Use case statements with pattern matching
- **Fiber scheduler:** Implement async programming with fibers
- **Hash syntax:** Use modern hash literal syntax consistently
- **Method chaining:** Leverage Enumerator and lazy evaluation

### Rails modernization

- **Zeitwerk autoloader:** Replace classic autoloader with Zeitwerk
- **Credentials management:** Use encrypted credentials instead of secrets
- **API responses:** Modernize API response formats and status codes
- **Database handling:** Use modern ActiveRecord patterns and query methods
- **Frontend integration:** Adopt modern frontend build tools and patterns

### Quality assurance

- **Automated testing:** Maintain comprehensive test suites
- **Static analysis:** Use RuboCop and other linting tools
- **Security scanning:** Run Brakeman and other security scanners
- **Performance testing:** Validate performance improvements
- **Integration testing:** Test all external service integrations

### Common pitfalls to avoid

- **Keyword argument changes:** Understand Ruby 3 keyword argument behavior
- **Hash merging:** Update hash merging syntax and methods
- **Gem compatibility:** Verify all gems support target Ruby version
- **Database adapters:** Update database drivers and adapters
- **Deployment issues:** Update deployment scripts and configurations

## Success story: E-commerce platform [#case-study]

A retail company modernized their Rails monolith:

- **Scope:** 300K lines of Ruby 2.5 + Rails 4.2 code
- **Target:** Ruby 3.2 + Rails 7.0 + microservices
- **Timeline:** 10 months phased migration
- **Results:** 3x performance improvement, enhanced security
- **Architecture:** Split into microservices with API gateway
