Project

typecheck

0.02
No commit activity in last 3 years
No release in over 3 years
Type checking for Ruby methods.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
 Dependencies

Development

~> 10.4
~> 3
 Project Readme

Gem Version Build Status Code Climate

Typecheck

Type checking for Ruby methods.

Validate the arguments and return value of a function, based on a type signature. Supports duck-type checking.

Example below is for Ruby 2.1, for 2.0 and earlier pass the function name explicitly :

def foo(..)
end
typecheck '...', :foo

Features by example:

require 'typecheck'

class Checked
  extend Typecheck

  typecheck 'Numeric -> Numeric',
  def double_me(num)
    num + num
  end

  typecheck 'String, Symbol -> Fixnum',
  def strsym_num(str, sym)
    str.length
  end

  # Duck typing FTW!
  typecheck '#to_str -> Symbol',
  def duck(str)
    str.to_str.upcase.intern
  end

  typecheck '#begin;#end -> Symbol',
  def multi(range)
    ('x' * range.end).chars.drop(range.begin).join.intern
  end

  typecheck '#to_str|Fixnum -> Symbol',
  def choice(x)
    :foo
  end

  typecheck '[Fixnum],[String] -> Numeric',
  def arrays(nums, strings)
    (nums + strings.map(&:length)).inject(:+)
  end

  typecheck 'Fixnum,String,Symbol -> Numeric',
  def optional(num, str = nil, sym = nil)
    num
  end
end

LICENSE

Copyright (c) 2014 Arne Brasseur, MIT License. See LICENSE file.