Code Monkey home page Code Monkey logo

contracts.ruby's Introduction

contracts.ruby 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.

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 rake

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 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1, 2.2, and jruby (both 1.8 and 1.9 modes).

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.

contracts.ruby's People

Contributors

waterlink avatar egonschiele avatar gsinclair avatar sfcgeorge avatar nixpulvis avatar alex-fedorov avatar yonkeltron avatar justincampbell avatar pikachuexe avatar tricknotes avatar kliuless avatar beezee avatar roryokane avatar abevoelker avatar freemanoid avatar lolmaus avatar chuckd avatar eval avatar betamatt avatar actsasbuffoon avatar robnormal avatar gitter-badger avatar fay-jai avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.