ruby-object-design_skill

This skill guides you to choose the right Ruby construct (class, module, Struct, or Data) by prioritizing objects and messages over default classes.

3

GitHub Stars

3

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill ag0os/rails-dev-plugin --skill ruby-object-design

  • class-vs-module.md9.3 KB
  • data-structures.md9.1 KB
  • SKILL.md9.0 KB

Overview

This skill helps you choose the right Ruby construct—class, module, Struct/Data, or Hash—by applying the principle that Ruby is object-oriented, not class-oriented. It discourages reflexively using class as the default and focuses on objects and messages. The guidance is practical and targeted at design decisions, refactors, and reviews to reduce boilerplate and over-engineering.

How this skill works

When triggered it inspects intent: whether multiple instances with encapsulated state are required, whether behavior accompanies that state, or whether the code is stateless utility or simple data. It applies a decision tree (class for object factories; module for stateless utilities; Struct/Data for simple data containers; Hash for ad-hoc groups) and flags common 'class smells'. Recommendations include concrete before/after patterns and a safe migration path.

When to use it

  • Deciding between class, module, Struct/Data, or Hash for a new component
  • Reviewing code that has many single-method or stateless classes
  • Refactoring pattern-named classes (Factory, Builder, Decorator) to simpler Ruby idioms
  • Designing features where object validity at construction matters
  • Converting boilerplate POROs into Structs or Data objects

Best practices

  • Only use class when you need an object factory: multiple instances with state + behavior
  • Prefer module with extend self for stateless utility collections and namespacing
  • Use Struct or Ruby 3.2+ Data for simple data containers and immutable value objects
  • Avoid single-method service classes; prefer module functions or plain methods when state is unnecessary
  • Ensure objects are valid immediately after .new; require needed data in initialize

Example use cases

  • A Policy domain object with state and behavior → keep as a class (object factory)
  • String utility methods currently in a class with only class methods → convert to module with extend self
  • A simple DTO used to pass attributes around → use Struct or Data instead of a full class
  • A CalculateDiscount service with only call → replace with module function or plain method
  • Legacy Report class requiring setters after .new → make constructor accept required data so object is valid at birth

FAQ

Use Struct for small mutable containers and Data (Ruby 3.2+) for immutable value objects; prefer Data for built-in immutability if your Ruby version supports it.

Are pattern-named classes always wrong?

Not always, but patterns like Factory/Builder/Decorator often indicate a design translated from other languages; re-evaluate if Ruby provides a simpler idiom before implementing the pattern.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational