ruby-overload 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 245e0e2cad16c3f0bdb7d0d772adac2b3f1a33297b44e28eab5b980970f6797e
4
+ data.tar.gz: 89b475ad0da4d5af3a0b0cdc9884c3dd95503d01063d494ba1437d154add7cc6
5
+ SHA512:
6
+ metadata.gz: b80121742f6486d36526077f24b56203784b4ee06f8eb22da61dd5eb40d078d93af9aeaf088d38d10e38a0eb3edc4162ae6edc2b80be4a75bb2d6ab9be39b133
7
+ data.tar.gz: 0dd581b7b939715877185ea8aa33d9ed1032dc3a397e4fe6e6f2ed5bacaf84ca813e476eb33a28828ba5efbcc0264ff39b0ade1aa5eef99c30d9090432747ae0
@@ -0,0 +1,3 @@
1
+ ### 0.1.0 (07/25/2020)
2
+
3
+ * Initial public release - [@jodosha](https://github.com/jodosha), [@baweaver](https://gist.github.com/baweaver), [@dblock](https://github.com/dblock).
@@ -0,0 +1,125 @@
1
+ # Contributing to Ruby-Overload
2
+
3
+ This project is work of [many contributors](https://github.com/dblock/ruby-overload/graphs/contributors).
4
+
5
+ You're encouraged to submit [pull requests](https://github.com/dblock/ruby-overload/pulls), [propose features and discuss issues](https://github.com/dblock/ruby-overload/issues).
6
+
7
+ In the examples below, substitute your Github username for `contributor` in URLs.
8
+
9
+ ### Fork the Project
10
+
11
+ Fork the [project on Github](https://github.com/dblock/ruby-overload) and check out your copy.
12
+
13
+ ```
14
+ git clone https://github.com/contributor/ruby-overload.git
15
+ cd ruby-overload
16
+ git remote add upstream https://github.com/dblock/ruby-overload.git
17
+ ```
18
+
19
+ ### Bundle Install and Test
20
+
21
+ Ensure that you can build the project and run tests.
22
+
23
+ ```
24
+ bundle install
25
+ bundle exec rake
26
+ ```
27
+
28
+ ## Contribute Code
29
+
30
+ ### Create a Topic Branch
31
+
32
+ Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
33
+
34
+ ```
35
+ git checkout master
36
+ git pull upstream master
37
+ git checkout -b my-feature-branch
38
+ ```
39
+
40
+ ### Write Tests
41
+
42
+ Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build. Add tests to [spec](spec).
43
+
44
+ We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
45
+
46
+ ### Write Code
47
+
48
+ Implement your feature or bug fix.
49
+
50
+ Ruby style is enforced with [Rubocop](https://github.com/bbatsov/rubocop). Run `bundle exec rubocop` and fix any style issues highlighted, auto-correct issues when possible with `bundle exec rubocop -a`. To silence generally ingored issues, including line lengths or code complexity metrics, run `bundle exec rubocop --auto-gen-config`.
51
+
52
+ Make sure that `bundle exec rake` completes without errors.
53
+
54
+ ### Write Documentation
55
+
56
+ Document any external behavior in the [README](README.md).
57
+
58
+ ### Update Changelog
59
+
60
+ Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*. Don't remove *Your contribution here*.
61
+
62
+ Make it look like every other line, including a link to the issue being fixed, your name and link to your Github account.
63
+
64
+ ### Commit Changes
65
+
66
+ Make sure git knows your name and email address:
67
+
68
+ ```
69
+ git config --global user.name "Your Name"
70
+ git config --global user.email "contributor@example.com"
71
+ ```
72
+
73
+ Writing good commit logs is important. A commit log should describe what changed and why.
74
+
75
+ ```
76
+ git add ...
77
+ git commit
78
+ ```
79
+
80
+ ### Push
81
+
82
+ ```
83
+ git push origin my-feature-branch
84
+ ```
85
+
86
+ ### Make a Pull Request
87
+
88
+ Go to https://github.com/contributor/ruby-overload and select your feature branch. Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
89
+
90
+ ### Update CHANGELOG Again
91
+
92
+ Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows.
93
+
94
+ ```
95
+ * [#123](https://github.com/dblock/ruby-overload/pull/123): Reticulated splines - [@contributor](https://github.com/contributor).
96
+ ```
97
+
98
+ Amend your previous commit and force push the changes.
99
+
100
+ ```
101
+ git commit --amend
102
+ git push origin my-feature-branch -f
103
+ ```
104
+
105
+ ### Rebase
106
+
107
+ If you've been working on a change for a while, rebase with upstream/master.
108
+
109
+ ```
110
+ git fetch upstream
111
+ git rebase upstream/master
112
+ git push origin my-feature-branch -f
113
+ ```
114
+
115
+ ### Check on Your Pull Request
116
+
117
+ Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
118
+
119
+ ### Be Patient
120
+
121
+ It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang on there!
122
+
123
+ ## Thank You
124
+
125
+ Please do know that we really appreciate and value your time and work. We love you, really.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'rake'
8
+
9
+ group :development, :test do
10
+ gem 'benchmark-ips'
11
+ gem 'rspec', '~> 3.0'
12
+ gem 'rubocop', '0.88.0'
13
+ end
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-overload (0.1.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ ast (2.4.1)
10
+ benchmark-ips (2.8.2)
11
+ diff-lcs (1.3)
12
+ parallel (1.19.2)
13
+ parser (2.7.1.4)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.0.0)
16
+ rake (13.0.1)
17
+ regexp_parser (1.7.1)
18
+ rexml (3.2.4)
19
+ rspec (3.9.0)
20
+ rspec-core (~> 3.9.0)
21
+ rspec-expectations (~> 3.9.0)
22
+ rspec-mocks (~> 3.9.0)
23
+ rspec-core (3.9.1)
24
+ rspec-support (~> 3.9.1)
25
+ rspec-expectations (3.9.1)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-mocks (3.9.1)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.9.0)
31
+ rspec-support (3.9.2)
32
+ rubocop (0.88.0)
33
+ parallel (~> 1.10)
34
+ parser (>= 2.7.1.1)
35
+ rainbow (>= 2.2.2, < 4.0)
36
+ regexp_parser (>= 1.7)
37
+ rexml
38
+ rubocop-ast (>= 0.1.0, < 1.0)
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (>= 1.4.0, < 2.0)
41
+ rubocop-ast (0.2.0)
42
+ parser (>= 2.7.0.1)
43
+ ruby-progressbar (1.10.1)
44
+ unicode-display_width (1.7.0)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ benchmark-ips
51
+ rake
52
+ rspec (~> 3.0)
53
+ rubocop (= 0.88.0)
54
+ ruby-overload!
55
+
56
+ BUNDLED WITH
57
+ 2.1.4
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Luca Guidi, Brandon Weaver, Daniel Doubrovkine and contributors.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ Ruby::Overload
2
+ ==========
3
+
4
+ [![Gem Version](http://img.shields.io/gem/v/ruby-overload.svg)](http://badge.fury.io/rb/ruby-overload)
5
+ [![Build Status](https://travis-ci.org/dblock/ruby-overload.svg?branch=master)](https://travis-ci.org/dblock/ruby-overload)
6
+
7
+ Runtime method overloading behavior for Ruby from [this blog post](https://lucaguidi.com/2020/07/22/ruby-method-overloading/) by [@jodosha](https://www.github.com/jodosha). Incorporates changes from [ptolemybarnes/overlord](https://github.com/ptolemybarnes/overlord), performance improvements from [baweaver@](https://gist.github.com/jodosha/e3097ed693e9b7c255b658ac39c2e403#gistcomment-3388594), and other ideas from [this tweet](https://twitter.com/jodosha/status/1285850349845254144).
8
+
9
+ ## Disclaimer
10
+
11
+ The original author of this idea, Luca Guidi, [does not recommend](https://twitter.com/jodosha/status/1285941282381139969) using this in production, but YOLO.
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ require 'ruby-overload'
17
+
18
+ class Foo
19
+ include Ruby::Overload
20
+
21
+ def call
22
+ puts "foo"
23
+ end
24
+
25
+ def call(arg)
26
+ puts "foo #{arg}"
27
+ end
28
+ end
29
+
30
+ foo = Foo.new
31
+ foo.call # => "foo"
32
+ foo.call(23) # => "foo 23"
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ You're encouraged to contribute to this gem. See [CONTRIBUTING](CONTRIBUTING.md) for details.
38
+
39
+ ## Copyright and License
40
+
41
+ Copyright (c) 2020, Luca Guidi, Brandon Weaver, Daniel Doubrovkine and [Contributors](CHANGELOG.md).
42
+
43
+ This project is licensed under the [MIT License](LICENSE.md).
44
+
45
+ ## Similar Projects
46
+
47
+ * [overloader](https://github.com/pocke/overloader)
@@ -0,0 +1,60 @@
1
+ # Releasing Ruby-Overload
2
+
3
+ There are no hard rules about when to release ruby-overload. Release bug fixes frequently, features not so frequently and breaking API changes rarely.
4
+
5
+ ### Release
6
+
7
+ Run tests, check that all tests succeed locally.
8
+
9
+ ```
10
+ bundle install
11
+ rake
12
+ ```
13
+
14
+ Check that the last build succeeded in [Travis CI](https://travis-ci.org/dblock/ruby-overload) for all supported platforms.
15
+
16
+ Add a date to this release in [CHANGELOG.md](CHANGELOG.md).
17
+
18
+ ```
19
+ ### 0.2.2 (2015/7/10)
20
+ ```
21
+
22
+ Remove the line with "Your contribution here.", since there will be no more contributions to this release.
23
+
24
+ Commit your changes.
25
+
26
+ ```
27
+ git add README.md CHANGELOG.md lib/ruby-overload/version.rb
28
+ git commit -m "Preparing for release, 0.2.2."
29
+ ```
30
+
31
+ Release.
32
+
33
+ ```
34
+ $ rake release
35
+
36
+ ruby-overload 0.2.2 built to pkg/ruby-overload-0.2.2.gem.
37
+ Tagged v0.2.2.
38
+ Pushed git commits and tags.
39
+ Pushed ruby-overload 0.2.2 to rubygems.org.
40
+ ```
41
+
42
+ ### Prepare for the Next Version
43
+
44
+ Add the next release to [CHANGELOG.md](CHANGELOG.md).
45
+
46
+ ```
47
+ ### 0.2.3 (Next)
48
+
49
+ * Your contribution here.
50
+ ```
51
+
52
+ Increment the third version number in [lib/ruby-overload/version.rb](lib/ruby-overload/version.rb).
53
+
54
+ Commit your changes.
55
+
56
+ ```
57
+ git add CHANGELOG.md lib/ruby-overload/version.rb
58
+ git commit -m "Preparing for next development iteration, 0.2.3."
59
+ git push origin master
60
+ ```
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+ require 'bundler/gem_tasks'
5
+
6
+ Bundler.setup :default, :development
7
+
8
+ require 'rspec/core'
9
+ require 'rspec/core/rake_task'
10
+
11
+ RSpec::Core::RakeTask.new(:spec) do |spec|
12
+ spec.pattern = FileList['spec/**/*_spec.rb']
13
+ end
14
+
15
+ require 'rubocop/rake_task'
16
+ RuboCop::RakeTask.new(:rubocop)
17
+
18
+ task default: %i[rubocop spec]
@@ -0,0 +1,29 @@
1
+ require 'benchmark/ips'
2
+ require 'ruby-overload'
3
+
4
+ class Foo
5
+ include Ruby::Overload
6
+
7
+ def call(number)
8
+ "foo #{number}"
9
+ end
10
+
11
+ def call
12
+ 'foo 42'
13
+ end
14
+ end
15
+
16
+ class Bar
17
+ def call(number)
18
+ "bar #{number}"
19
+ end
20
+ end
21
+
22
+ foo = Foo.new
23
+ bar = Bar.new
24
+
25
+ Benchmark.ips do |x|
26
+ x.report('method overloading') { foo.call(23) }
27
+ x.report('method') { bar.call(23) }
28
+ x.compare!
29
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby-overload/version'
4
+ require 'ruby-overload/overload'
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruby
4
+ module Overload
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def method_added(method_name)
11
+ if @_recurse_catch
12
+ @_recurse_catch = nil
13
+ return
14
+ end
15
+
16
+ original_method = instance_method(method_name)
17
+
18
+ @_matches ||= Hash.new { |h, k| h[k] = {} }
19
+ @_matches[method_name][original_method.arity] = original_method
20
+ undef_method method_name
21
+
22
+ # Prevent recursive calls to method_added if we're doing it
23
+ # intentionally here.
24
+ @_recurse_catch = true
25
+
26
+ # Localize for closure
27
+ method_matches = @_matches[method_name]
28
+
29
+ if @_methods && @_methods[method_name]
30
+ define_method(method_name, @_methods[method_name])
31
+ else
32
+ define_method(method_name) do |*as|
33
+ method_matches[as.size].bind(self).call(*as)
34
+ end
35
+
36
+ @_methods ||= {}
37
+ @_methods[method_name] = instance_method(method_name)
38
+ end
39
+ end
40
+ end
41
+
42
+ def method_missing(method_name, *args, &blk)
43
+ super unless self.class.respond_to_matching?(method_name, *args, &blk)
44
+
45
+ self.class.matched_call(self, method_name, *args, &blk)
46
+ end
47
+
48
+ def respond_to_missing?(method_name, *)
49
+ self.class.respond_to_method?(method_name)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruby
4
+ module Overload
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby-overload'
Binary file
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.push File.expand_path('lib', __dir__)
4
+ require 'ruby-overload/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'ruby-overload'
8
+ s.version = Ruby::Overload::VERSION
9
+ s.authors = ['Luca Guidi', 'Brandon Weaver', 'Daniel Doubrovkine']
10
+ s.email = 'dblock@dblock.org'
11
+ s.platform = Gem::Platform::RUBY
12
+ s.required_ruby_version = '>= 2.4'
13
+ s.required_rubygems_version = '>= 1.3.6'
14
+ s.files = Dir['**/*']
15
+ s.require_paths = ['lib']
16
+ s.homepage = 'http://github.com/dblock/ruby-overload'
17
+ s.licenses = ['MIT']
18
+ s.summary = 'Method overload behavior for Ruby.'
19
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Ruby::Overload do
6
+ let(:klass) do
7
+ Class.new do
8
+ include Ruby::Overload
9
+
10
+ def call
11
+ nil
12
+ end
13
+
14
+ def call(arg)
15
+ arg
16
+ end
17
+
18
+ def call(first, second)
19
+ [first, second]
20
+ end
21
+ end
22
+ end
23
+ subject do
24
+ klass.new
25
+ end
26
+ it 'matches on a method with no arguments' do
27
+ expect(subject.call).to be nil
28
+ end
29
+ it 'matches on methods with one argument' do
30
+ expect(subject.call(1)).to eq 1
31
+ end
32
+ it 'matches on methods with two arguments' do
33
+ expect(subject.call(%w[a b])).to eq(%w[a b])
34
+ end
35
+ it 'raises undefined method when method is not defined' do
36
+ expect { subject.call 1, 2, 3 }.to raise_error NoMethodError
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Ruby::Overload do
6
+ it 'has a version' do
7
+ expect(Ruby::Overload::VERSION).not_to be_nil
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+
5
+ require 'rubygems'
6
+ require 'rspec'
7
+ require 'ruby-overload'
8
+
9
+ RSpec.configure(&:raise_errors_for_deprecations!)
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-overload
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luca Guidi
8
+ - Brandon Weaver
9
+ - Daniel Doubrovkine
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2020-07-25 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description:
16
+ email: dblock@dblock.org
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - CHANGELOG.md
22
+ - CONTRIBUTING.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.md
26
+ - README.md
27
+ - RELEASING.md
28
+ - Rakefile
29
+ - benchmarks/overload.rb
30
+ - lib/ruby-overload.rb
31
+ - lib/ruby-overload/overload.rb
32
+ - lib/ruby-overload/version.rb
33
+ - lib/ruby_overload.rb
34
+ - pkg/ruby-enum-0.8.0.gem
35
+ - ruby-overloard.gemspec
36
+ - spec/ruby-overload/overload_spec.rb
37
+ - spec/ruby-overload/version_spec.rb
38
+ - spec/spec_helper.rb
39
+ homepage: http://github.com/dblock/ruby-overload
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '2.4'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 1.3.6
57
+ requirements: []
58
+ rubygems_version: 3.1.2
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Method overload behavior for Ruby.
62
+ test_files: []