Giter Club home page Giter Club logo

invariant's Introduction

Invariant

Invariant is a simple gem that provides Kernel#assert to document your assumptions in code.

Use assertions to prevent the impossible.

Whenever you find yourself thinking "but of course that could never happen," add code to check it. The easiest way to do this is with assertions.

http://pragmatictips.com/33

Installation

Add to Gemfile:

gem 'invariant'

Configuration

Assertions are enabled by default. Better safe than sorry. To disable assertions:

Invariant.disable_assertions

For example, in Rails you may want to disable assertions in Production:

# config/initializers/invariant.rb
Invariant.disable_assertions if Rails.env.production?

Usage

Use assert to test a condition:

assert age > 0

Provide an optional message:

assert errors.empty?, "Why do we still have errors?"

You can also test a block of code. This is handy when you're invariant requires several lines of code.

assert do
  one_thing = calculate_something
  other_thing = calculate_something_else
  one_thing > other_thing
end

Blocks also support an optional message:

assert 'That one thing should always be greater' do
  one_thing = calculate_something
  other_thing = calculate_something_else
  one_thing > other_thing
end

Errors

A failed assertion raises Invariant::AssertionError which inherits directly from Exception.

An AssertionError should be an exceptional failure. It should not be something a program knows how to recover from. This is why AssertionError is not a StandardError:

  def real_world
    assert 1 == 0, 'We are in the real world'
  rescue
    'We are in the Matrix'
  end

  real_world # => raises Invariant::AssertionError, 'We are in the real world'

invariant's People

Contributors

pithyless avatar obrok avatar

Stargazers

Michael Geiger avatar Felipe Lima avatar milk1000cc avatar Kyrylo Silin avatar

Watchers

 avatar James Cloos avatar

Forkers

obrok

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.