Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Ruby
  3. Virtus

Virtus

MITRubyv2.0.0

A Ruby library for defining attributes with type coercion, default values, and embedded objects on plain Ruby objects.

GitHubGitHub
3.7k stars231 forks0 contributors

What is Virtus?

Virtus is a Ruby library that adds attribute definitions with type coercion and default values to plain Ruby objects. It solves the problem of manually handling data conversion and validation by allowing developers to define typed attributes that automatically coerce input values, such as converting strings to integers or hashes to nested objects. This makes it easier to work with external data sources like web parameters or JSON APIs.

Target Audience

Ruby developers building applications that require structured data handling, such as web APIs, domain models, or value objects, especially those needing type-safe attribute assignment and coercion.

Value Proposition

Developers choose Virtus for its simplicity and flexibility in adding attribute management to any Ruby object without the overhead of a full ORM. Its support for embedded objects, collections, and custom coercions provides a powerful yet lightweight solution for data mapping and validation.

Overview

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects

Use Cases

Best For

  • Sanitizing and coercing input parameters in Ruby web applications
  • Mapping JSON API responses to domain objects with nested structures
  • Creating value objects with typed attributes and default values
  • Prototyping domain models quickly with attribute definitions
  • Handling collections of typed objects with automatic coercion
  • Building configuration objects with type-safe attribute assignment

Not Ideal For

  • New projects requiring active maintenance and security updates
  • Applications needing built-in validation alongside coercion
  • Teams using Ruby 3+ or modern frameworks like Hanami that prefer dry-rb integrations
  • Systems where mutation of collections must automatically trigger coercion

Pros & Cons

Pros

Flexible Attribute DSL

Allows defining typed attributes on classes, modules, or instances with options like private writers, as shown in examples for User and Address classes.

Robust Type Coercion

Automatically converts inputs like strings to integers or hashes to nested objects, supporting many coercions including custom ones via Virtus::Attribute subclasses.

Embedded Object Support

Handles complex nested structures with automatic coercion, making it ideal for JSON mapping, as demonstrated with City and Address embedded values.

Default Value Flexibility

Supports static values, procs, or method names for defaults, with lazy loading options for dynamic instances.

Cons

Discontinued and Unmaintained

The author explicitly marks it as discontinued, recommending dry-rb gems as successors, so it lacks updates and community support.

Mutation Coercion Gap

As noted in the README, coercions don't apply when mutating collections after assignment (e.g., using <<), requiring manual workarounds like custom collection classes.

Boolean Type Conflicts

Susceptible to issues with global Boolean constants, forcing workarounds like using Axiom::Types::Boolean, which adds complexity.

Limited Validation Features

Focuses on coercion without built-in validation, so additional gems or custom logic are needed for comprehensive data integrity.

Frequently Asked Questions

Quick Stats

Stars3,745
Forks231
Contributors0
Open Issues49
Last commit5 years ago
CreatedSince 2011

Tags

#object-mapper#ruby-gem#data-mapping#type-coercion#value-objects#attributes#domain-modeling#ruby#input-sanitization

Built With

R
Ruby

Included in

Ruby14.1k
Auto-fetched 1 day ago

Related Projects

HashieHashie

Hashie is a collection of classes and mixins that make Ruby hashes more powerful.

Stars3,039
Forks315
Last commit11 days ago
HamsterHamster

Efficient, Immutable, Thread-Safe Collection classes for Ruby

Stars1,896
Forks93
Last commit4 years ago
AddressableAddressable

Addressable is an alternative implementation to the URI implementation that is part of Ruby's standard library. It is flexible, offers heuristic parsing, and additionally provides extensive support for IRIs and URI templates.

Stars1,609
Forks283
Last commit3 months ago
ActiveAttrActiveAttr

What ActiveModel left out

Stars1,194
Forks90
Last commit7 months ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub