Ruby coder

From majesticlabs-dev/majestic-marketplace · View interactive version

Ruby coder is an AI agent skill from the GitHub repository majesticlabs-dev/majestic-marketplace. This skill guides writing of new Ruby code following modern Ruby 3.x syntax, Sandi Metz's 4 Rules for Developers, and idiomatic Ruby best practices. Use when creating new Ruby files, writing Ruby methods, or refactoring Ruby code to ensure adherence to clarity, simplicity, and maintainability standa

How to use this skill

Skill file

Ruby Coder

Ruby 3.x Modern Syntax

Use hash shorthand when keys match variable names:

age = 49
name = "David"
user = { name:, age: }

For general naming conventions, semantic methods, and enumerable patterns, see references/ruby-style-conventions.md.

Sandi Metz's 4 Rules for Developers

These rules enforce strict limits to maintain code quality. Breaking them requires explicit justification.

Rule 1: Classes Can Be No Longer Than 100 Lines

Limit: Maximum 100 lines of code per class

Check: When a class exceeds this limit, extract secondary concerns to new classes

# When exceeding 100 lines, extract secondary concerns:
class UserProfilePresenter  # Presentation logic
class UserProfileValidator  # Validation logic
class UserProfileNotifier   # Notification logic

Exceptions: Valid Single Responsibility Principle (SRP) justification required

Rule 2: Methods Can Be No Longer Than 5 Lines

Limit: Maximum 5 lines per method. Each if/else branch counts as lines.

# Good - 5 lines or fewer
def process_order
  validate_order
  calculate_totals
  apply_discounts
  finalize_payment
end

# Avoid - extract when too long
def process_order
  return unless items.any?
  return unless valid_address?
  self.subtotal = items.sum(&:price)
  self.tax = subtotal * tax_rate
  self.total = subtotal + tax
  charge_customer
  send_confirmation
end

Exceptions: Pair approval required (Rule 0: break rules with agreement)

Rule 3: Pass No More Than 4 Parameters

Limit: Maximum 4 parameters per method. Use parameter objects or hashes when more data is needed.

# Parameter object when data is related
def create_post(post_params)
  Post.create(user: post_params.user, title: post_params.title, content: post_params.content)
end

Exceptions: Rails view helpers like link_to or form_for are exempt

Rule 4: Controllers May Instantiate Only One Object

Limit: Controllers should instantiate only one object; other objects come through that via facade pattern.

# Good - single object via facade
class DashboardController < ApplicationController
  def show
    @dashboard = DashboardFacade.new(current_user)
  end
end

# Avoid - multiple instance variables
class DashboardController < ApplicationController
  def show
    @user = current_user
    @posts = @user.posts.recent
    @notifications = @user.notifications.unread
  end
end

Code Quality Standards

Thread Safety

Use Mutex for shared mutable state:

class Configuration
  @instance_mutex = Mutex.new

  def self.instance
    return @instance if @instance
    @instance_mutex.synchronize { @instance ||= new }
  end
end

Idempotent Operations

Design operations to be safely repeatable:

def activate_user
  return if user.active?
  user.update(active: true)
  send_activation_email unless email_sent?
end

Refactoring Triggers

Extract classes when:

Extract methods when:

Use parameter objects when:

Create facades when:

When to Break Rules

Sandi Metz's "Rule 0": Break any of the 4 rules only with pair approval or clear justification.

| Rule | Valid Exception |

|------|---------------|

| 100 lines | Clear SRP justification required |

| 5 lines | Complex but irreducible algorithms |

| 4 params | Rails view helpers exempt |

| 1 object | Simple views without facades |

Document all exceptions with clear reasoning in code comments.

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.