GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - ddd-ruby/contracts.ruby: Contracts for Ruby methods

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

580 Commits
580 Commits

Folders and files

NameName
Last commit message
Last commit date
benchmarks
benchmarks
 
 
bin
bin
 
 
docs
docs
 
 
lib
lib
 
 
spec
spec
 
 
.codeclimate.yml
.codeclimate.yml
 
 
.gitignore
.gitignore
 
 
.rspec
.rspec
 
 
.rubocop.yml
.rubocop.yml
 
 
.travis.yml
.travis.yml
 
 
CHANGELOG.markdown
CHANGELOG.markdown
 
 
Gemfile
Gemfile
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
Rakefile
Rakefile
 
 
TUTORIAL.md
TUTORIAL.md
 
 
contracts.gemspec
contracts.gemspec
 
 

Repository files navigation

contracts.ruby

Build Status Gem Version Code Climate codecov Dependency Status

Fork of https://github.com/egonSchiele/contracts.ruby

Until this issue egonSchiele#246 is resolved, we decided to mainain our own fork of contracts named contracts-lite.


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-lite

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

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.9.2, 1.9.3, 2.0.0, 2.1, 2.2, and jruby (1.9 mode).

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.

About

Contracts for Ruby methods

egonschiele.github.com/contracts.ruby

Resources

Readme
BSD-2-Clause license
Activity
Custom properties

Stars

3 stars

Watchers

0 watching

Forks

1 fork
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.