The philosophy here is to bring as much modern language features that help developers code better, safer, and more reliably. Here are some top priorities of this library:

  • Immutability first with mutability as an option (e.g for speed)
  • Type safety
  • Follow functional practices
  • Use standard interfaces of Ruby
  • Fail early, throw fast


Add this line to your application's Gemfile:

gem 'generics'

And then execute:

$ bundle

Or install it yourself as:

$ gem install generics


list_of_ints = Generics::List[Integer].new(1, 2, 3, 4)
list_of_ints.add(5) # ok
list_of_ints.add('6') # NotSameType exception
list_of_ints.count # 4 (immutable)
list_of_ints.add(5).add(6).add(7).count # 7


After checking out the repo, run bin/setup to install dependencies. 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


  • Other collection types (at least the same list as core ruby data types and the Hamster immutable library)
  • More complex generics
    • Responds to generics (another form of duck type, though modules are preferable)
    • Array generics (e.g. list of strings)
    • Hash generics (e.g. keys are numbers and values are list of strings)
    • Either/enums (e.g. list of strings or integers of anything that responds to :x). This would require either adopting a library or creating one ourselves as a separate gem
  • Generics in functions/closures
  • Generics in methods
  • Generics in classes


Bug reports and pull requests are welcome on GitHub at[USERNAME]/generics. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


The gem is available as open source under the terms of the MIT License.