Coder Social home page Coder Social logo

contracts's Introduction

Hacked together a contracts implementation in Ruby.

require './contracts'

class Test
  extend Contracts

  add_contract { |num| num.respond_to? :to_i }
  .and         { |num| num < 100 }
  .to def is_even?(num)
    num % 2 == 0
  end

  add_contract { |array| array.respond_to? :to_a }
  .and         { |array| array.all? { |i| i.is_a? Numeric } }
  .and_ensure  { |result| result.all? { |i| i % 2 == 0 } }
  .to def times_by_two(array)
    array.map { |i| i * 3 }
  end
end

begin
  p Test.new.is_even?(10)
  p Test.new.is_even?(500)
rescue Exception => e
  puts e.message
end

begin
  p Test.new.times_by_two([1, 2, 3, 4])
rescue Exception => e
  puts e.message
end
$ test.rb

[]

Return contract on Test#is_even? failed:

 num < 100

Args: [500]


Return contract on Test#times_by_two failed:

 result.all? { |i| i % 2 == 0 }

Return value: [3, 6, 9, 12]
Args: [[1, 2, 3, 4]]

contracts's People

Contributors

widdershin avatar

Watchers

 avatar  avatar  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.