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
- Fork it ( https://github.com/[my-github-username]/type_hinting/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request