entitlements-app/lib/contracts-ruby3
GrantBirki f6a5490587
update all `actions/checkout` versions to v4
2024-03-18 09:46:22 -06:00
..
.github/workflows update all `actions/checkout` versions to v4 2024-03-18 09:46:22 -06:00
benchmarks Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
features Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
lib Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
script Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
spec Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
CHANGELOG.markdown Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
Gemfile Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
LICENSE Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
README.md Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
Rakefile Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
TODO.markdown Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
TUTORIAL.md Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
contracts.gemspec Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
cucumber.yml Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00
dependabot.yml Vendor two versions of contracts.ruby and drop from gemspec/Gemfile.lock 2022-08-09 12:30:46 -05:00

README.md

This project is looking for a new maintainer! More details here

contracts.ruby GitHub Build Status Join the chat at https://gitter.im/egonSchiele/contracts.ruby

Contracts let you clearly – even beautifully – express how your code behaves, and free you from writing tons of boilerplate, defensive code.

You can think of contracts as assert on steroids.

0.17.x = Ruby 3.x only

0.17.x only supports Ruby 3.x
Looking for Ruby 2.x support?
Use 0.16.x

Installation

gem install contracts

Hello World

A contract is one line of code that you write above a method definition. It validates the arguments to the method, and validates the return value of the method.

Here is a simple contract:

  Contract Num => Num
  def double(x)

This says that double expects a number and returns a number. Here's the full code:

require 'contracts'

class Example
  include Contracts::Core
  include Contracts::Builtin

  Contract Num => Num
  def double(x)
    x * 2
  end
end

puts Example.new.double("oops")

Save this in a file and run it. Notice we are calling double with "oops", which is not a number. The contract fails with a detailed error message:

ParamContractError: Contract violation for argument 1 of 1:
        Expected: Num,
        Actual: "oops"
        Value guarded in: Example::double
        With Contract: Num => Num
        At: main.rb:8
        ...stack trace...

Instead of throwing an exception, you could log it, print a clean error message for your user...whatever you want. contracts.ruby is here to help you handle bugs better, not to get in your way.

Tutorial

Check out this awesome tutorial.

Use Cases

Check out this screencast.

Development

To get started do the following:

  1. Install required gems for development

bundle install

  1. Run our test suite

bundle exec rspec

  1. Run our code style checks

bundle exec rubocop

Performance

Using contracts.ruby results in very little slowdown. Check out this blog post for more info.

Q. What Rubies can I use this with?

A. It's been tested with 2.1, 2.2, 2.3, 2.4, 2.5, 2.6 and 2.7.

If you're using the library, please let me know what project you're using it on :)

Testimonials

Contracts literally saves us hours of pain at Snowplow every day

Alexander Dean, creator of Snowplow

Contracts caught a bug that saved us several hundred dollars. It took less than 30 seconds to add the contract.

Michael Tomer

Credits

Inspired by contracts.coffee.

Copyright 2012-2015 Aditya Bhargava. Major improvements by Alexey Fedorov.

BSD Licensed.