Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pocke/overloader
Overload for Ruby
https://github.com/pocke/overloader
Last synced: about 2 months ago
JSON representation
Overload for Ruby
- Host: GitHub
- URL: https://github.com/pocke/overloader
- Owner: pocke
- License: cc0-1.0
- Created: 2019-04-27T14:49:38.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-01-25T12:46:32.000Z (almost 5 years ago)
- Last Synced: 2024-10-13T21:29:39.372Z (3 months ago)
- Language: Ruby
- Homepage:
- Size: 33.2 KB
- Stars: 39
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Overloader
Overload for Ruby
# DO NOT USE THIS LIBRARY FOR PRODUCTION
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'overloader'
```And then execute:
$ bundle install
Or install it yourself as:
$ gem install overloader
## Usage
```ruby
require 'overloader'class A
extend Overloader
overload do
def foo() "no args" end
def foo(x) "one arg" end
def foo(x, y) "two args" end
end
enda = A.new
p a.foo # => "no args"
p a.foo(1) # => "one args"
p a.foo(1, 2) # => "two args"
```## Advanced Usage: types
You can define overload with types.
This feature requires the following things.
* Ruby 2.7 or greater
* [ruby-signature](https://github.com/ruby/ruby-signature) gem
* This gem has not been published to RubyGems.org. So you need to install it manually.First, add `require 'overloader/type'`.
Then, define the method type with RBS syntax. https://github.com/ruby/ruby-signature```ruby
require 'overloader'
require 'overloader/type'class A
extend Overloader
overload do
# (String, Integer) -> untyped
def foo(x, y) 'str int' end# (Integer, String) -> untyped
def foo(x, y) 'int str' end# (Symbol, Symbol) -> untyped
def foo(x, y) 'sym sym' end
end
enda = A.new
p a.foo('bar', 42) # => "str int"
p a.foo(42, 'baz') # => "int str"
p a.foo(:a, :b) # => "sym sym"
p a.foo(:a, 42) # => ArgumentError
```## Development
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/pocke/overloader.