The project is in a healthy, maintained state
Comparable T::Struct's for the equality focused typed Ruby developer.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
 Dependencies

Development

>= 1.13
>= 10.0
>= 3.0
>= 0.5

Runtime

 Project Readme

Sorbet

Gem Version Continuous Integration

Making T::Struct's comparable since 2020

If you just want some simple equality checking on your T::Struct's then you've come to the right place.

Out of the box Sorbet's super useful T::Struct's (https://sorbet.org/docs/tstruct) are not comparable meaning two separate instances (i.e. different #object_id's) of a T::Struct with identical attributes are not equal.

This behaviour can be confusing at first glance, especially if/when you start using T::Struct's alongside RSpec where expectations such as it { is_expected.to eq my_expected_struct } will fail.

It makes sense for this comparable behaviour to not be baked into T::Struct's by default since defining what makes two instances of something equal could be seen by some as business logic, but for the 90% of cases where you just want to run <=> over your attributes this mixin is a solution.

Installation

Add this line to your application's Gemfile:

gem 'sorbet-struct-comparable'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install sorbet-struct-comparable

Usage

Just include T::Struct::ActsAsComparable in your struct, done!

require 'sorbet-struct-comparable'

class BlogPost < T::Struct
  include T::Struct::ActsAsComparable

  class Topic < T::Enum
    enums do
      Cycling = new
      Music = new
      Walking = new
    end
  end

  const :title, String
  const :topic, Topic
end

blog_post = BlogPost.new(title: 'My Blog Post', topic: BlogPost::Topic::Cycling)
different_blog_post = BlogPost.new(title: 'Another Blog Post', topic: BlogPost::Topic::Walking)
identical_blog_post = BlogPost.new(title: 'My Blog Post', topic: BlogPost::Topic::Cycling)

blog_post == different_blog_post
# => false
blog_post == identical_blog_post
# => true

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bellroy/sorbet-struct-comparable.