• Stars
    star
    1,441
  • Rank 32,667 (Top 0.7 %)
  • Language
    Ruby
  • License
    BSD 2-Clause "Sim...
  • Created over 12 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Contracts for Ruby.

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 3.0 and 3.1. (In case this list becomes outdated see .github/workflows/tests.yaml)

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.

More Repositories

1

grokking_algorithms

Code for the book Grokking Algorithms (https://amzn.to/29rVyHf)
JavaScript
9,470
star
2

mdpress

[DEAD] Make impress.js presentations from markdown files.
CSS
339
star
3

chips

A clone of Chips Challenge in Haskell
Haskell
140
star
4

HandsomeSoup

Easy HTML parsing for Haskell
Haskell
125
star
5

dominion

A Dominion simulator in Haskell
Haskell
109
star
6

actionkid

A video game framework for haskell
Haskell
46
star
7

salty

Language that transpiles to PHP, JavaScript, and JSX.
Haskell
43
star
8

chisel

Open source writing app
TypeScript
19
star
9

Django-EditArea

Syntax highlighting for the Django admin. This is a Django application that contains a widget to render a form field as an EditArea editor.
JavaScript
15
star
10

groupme-cli

A command-line client for GroupMe.
Haskell
15
star
11

GrabYourWallet

Chrome extension that lets you know if a site you are browsing supports Trump.
JavaScript
14
star
12

rdioh

Rdio API wrapper in Haskell
Haskell
13
star
13

Linear-C--

Linear Algebra for C++.
C++
11
star
14

OpenCV

*NOT* the original! This is my branch of OpenCV.
C++
8
star
15

salmon

A preprocessor for Ruby that adds some Haskell-like syntax
Haskell
8
star
16

Clutter

A quick-to-play language game for Android
Java
7
star
17

console_renderer

A console renderer for Redcarpet
Ruby
5
star
18

luacv

My own fork of luacv (http://sourceforge.net/projects/luacv/)
C++
5
star
19

beeline

Beeline extension for Google Chrome
JavaScript
5
star
20

twss.hs

twss.js to Haskell
Haskell
4
star
21

Schala

Scheme interpreter in Scala.
Scala
4
star
22

Github-Repo-Search

Find the clone url of a repo from the command line.
Ruby
4
star
23

Moon-Curry

Functional programming for Moonscript.
3
star
24

quickpin

Chrome extension to quickly pin things on Pinterest.
JavaScript
3
star
25

fast_struct

A fast implementation of OpenStruct
Ruby
3
star
26

radiobread

A script to generate band name + food puns
Ruby
3
star
27

obvious

A svbtle clone in Haskell.
JavaScript
3
star
28

light_server

Basic Sinatra clone for when you don't want to include a gem.
Ruby
3
star
29

ones

A 2048 clone in Haskell
Haskell
3
star
30

chorewheel

Easy chore scheduling in Ruby
Ruby
2
star
31

reval

Use codepad from the command line.
Ruby
2
star
32

server

http 2.0 experimentation
Ruby
2
star
33

reapply

An experiment with diffs.
Haskell
2
star
34

egonschiele.github.com

JavaScript
2
star
35

makegem

Base template for Ruby gems.
Ruby
2
star
36

smartcheck

Faux static-duck-type-checking for Ruby.
Ruby
1
star
37

Graphable

graph algorithms for Haskell
Haskell
1
star
38

Curry

Better functional programming in Ruby.
Ruby
1
star
39

image_captioner

Describe an image using the MS cognitive sciences API
Ruby
1
star
40

fuzzy_search

Local fuzzy search
Ruby
1
star
41

ritual

Information tracker
Ruby
1
star
42

luaquant

lua bindings for imagequant
C
1
star
43

salt-vim

Adds syntax highlighting and indent support for Salty in vim.
Vim Script
1
star
44

Crypto

My flash game in coffeescript.
JavaScript
1
star
45

nginx

My fork of nginx
C
1
star
46

utils

Utilities for various languages.
Haskell
1
star
47

defundthepolice

HTML
1
star
48

dist

Distributed Hadoop-type thing in Ruby
Ruby
1
star