0.0
No commit activity in last 3 years
No release in over 3 years
Get type safety in your Ruby code
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
>= 0
~> 10.0
>= 0
 Project Readme

TypeHinting

I sometimes find myself writing code like this:

  def do_something_with_argument(argument)
    unless argument.kind_of?(SomeClassImExpecting)
      raise Exception, 'Whatever'
    end
    do_more_stuff
  end

This gem provides a mixin which add two class methods, return_type and
param_types, that are a bit nicer to work with.

Installation

Add this line to your application's Gemfile:

gem 'type_hinting'

And then execute:

$ bundle

Or install it yourself as:

$ gem install type_hinting

Usage

Dead simple, here's the example used in the tests:

class TypeHinted
  include TypeHinting

  def return_should_work
    []
  end
  return_type(:return_should_work, Array)
  # raise if method returns anything other than array

  def return_should_raise
    nil
  end
  return_type(:return_should_raise, Array)
  # same as above

  def hinted_params(a, b = 4)
    [a, b]
  end
  param_types(:hinted_params, Array, Fixnum) 
  # raise if passed anything other than Array for arg1,
  # raise if passed anything other than Fixnum for arg2
  # nil is allowed for arg2 since there is a default value specified
end

Contributing

  1. Fork it ( https://github.com/[my-github-username]/type_hinting/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request