rb_function_overload 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +148 -0
- data/lib/rb_function_overload/dispatcher.rb +36 -0
- data/lib/rb_function_overload/registry.rb +62 -0
- data/lib/rb_function_overload/version.rb +5 -0
- data/lib/rb_function_overload.rb +56 -0
- metadata +78 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: dc52422a1eee1a53aaa0cc6e4b2d436b98c25433a69bfbe552ddd1f187abd1ec
|
|
4
|
+
data.tar.gz: acef08e7f4cf0039a23dc43af05bac4e4719f472654b0c9bfcfa3236fd40b4fe
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b0ff8a4aa3a42644cb66ba8d1d773507a7ad530fd4fd0b2facc7cc5ab239a4785d07c7faa9f6d36bc785c171e20633d6268828ab72a829af2f1290f6fcd2b9c2
|
|
7
|
+
data.tar.gz: fe0c40eb548ca38083e9496b25311c1150547063a194cba91752f737711a287ac12568fde60350eb0719c8096501267cada1e4e478078e881ca3e5fbb771444b
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ishaan Bhela
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# RbFunctionOverload
|
|
2
|
+
|
|
3
|
+
Argument-count-based method overloading for Ruby, with no runtime dependencies.
|
|
4
|
+
|
|
5
|
+
Ruby keeps only the last definition of a method name. RbFunctionOverload lets you
|
|
6
|
+
declare several implementations of the same instance method and picks the right
|
|
7
|
+
one from the **number of positional arguments** in the call.
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
require "rb_function_overload"
|
|
11
|
+
|
|
12
|
+
class Calculator
|
|
13
|
+
include RbFunctionOverload
|
|
14
|
+
|
|
15
|
+
overload(:add) { |a| a }
|
|
16
|
+
overload(:add) { |a, b| a + b }
|
|
17
|
+
overload(:add) { |a, b, c| a + b + c }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
calculator = Calculator.new
|
|
21
|
+
calculator.add(10) # => 10
|
|
22
|
+
calculator.add(10, 20) # => 30
|
|
23
|
+
calculator.add(10, 20, 30) # => 60
|
|
24
|
+
calculator.add(1, 2, 3, 4) # ArgumentError: wrong number of arguments for
|
|
25
|
+
# overloaded method `add' (given 4, expected 1, 2 or 3)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
gem "rb_function_overload"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Requires Ruby 3.0 or newer. The test suite has been run on Ruby 3.2.
|
|
35
|
+
|
|
36
|
+
## The DSL
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
overload(:name) { |a, b| ... }
|
|
40
|
+
overload :name do |a, b|
|
|
41
|
+
...
|
|
42
|
+
end
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Inside `do ... end`, you write ordinary Ruby—local variables, conditionals,
|
|
46
|
+
instance variables (`@ivars`), helper method calls, and `next` for early returns:
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
class PriceCalculator
|
|
50
|
+
include RbFunctionOverload
|
|
51
|
+
|
|
52
|
+
def initialize(tax_rate)
|
|
53
|
+
@tax_rate = tax_rate
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
overload :total do |amount|
|
|
57
|
+
next 0 if amount <= 0
|
|
58
|
+
|
|
59
|
+
with_tax(amount)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
overload :total do |amount, discount|
|
|
63
|
+
discounted = [amount - discount, 0].max
|
|
64
|
+
next 0 if discounted.zero?
|
|
65
|
+
|
|
66
|
+
subtotal = with_tax(discounted)
|
|
67
|
+
subtotal.round(2)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def with_tax(amount)
|
|
73
|
+
amount * (1 + @tax_rate)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
calc = PriceCalculator.new(0.25)
|
|
78
|
+
calc.total(-5) # => 0
|
|
79
|
+
calc.total(100) # => 125.0
|
|
80
|
+
calc.total(100, 15) # => 106.25
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
- The argument count is read from the block's parameters. There is no separate
|
|
84
|
+
count argument.
|
|
85
|
+
- Use `do ... end` or parenthesised `{ ... }`. Writing `overload :add { ... }`
|
|
86
|
+
attaches the braces to `:add` and is a syntax error. (`overload :add, do ... end`
|
|
87
|
+
with a comma is also a syntax error.)
|
|
88
|
+
- Blocks run with the receiving object as `self` (via `instance_exec`), so they can
|
|
89
|
+
read instance variables and call any instance method, including private ones.
|
|
90
|
+
- `overload` returns the method name as a Symbol.
|
|
91
|
+
|
|
92
|
+
## Errors
|
|
93
|
+
|
|
94
|
+
| Situation | Error |
|
|
95
|
+
| --- | --- |
|
|
96
|
+
| Called with a count that has no overload | `ArgumentError` |
|
|
97
|
+
| Same name and same count declared twice | `RbFunctionOverload::DuplicateOverloadError` |
|
|
98
|
+
| No block, or a signature outside v0.1 scope | `RbFunctionOverload::InvalidOverloadError` |
|
|
99
|
+
|
|
100
|
+
`DuplicateOverloadError` and `InvalidOverloadError` inherit from `RbFunctionOverload::Error`.
|
|
101
|
+
A rejected declaration changes nothing: the first rejected overload of a name does
|
|
102
|
+
not define the method, and a rejected duplicate leaves the original in place.
|
|
103
|
+
|
|
104
|
+
## Limitations (v0.1)
|
|
105
|
+
|
|
106
|
+
Only exact positional argument counts are supported. Not supported:
|
|
107
|
+
|
|
108
|
+
- type-based overloading
|
|
109
|
+
- optional or default parameters (`|a, b = 1|`), rejected with `InvalidOverloadError`
|
|
110
|
+
- keyword arguments, variadic `*args` and block parameters, also rejected
|
|
111
|
+
- class-method overloading
|
|
112
|
+
- inheritance beyond the basics (see below)
|
|
113
|
+
|
|
114
|
+
Behaviours worth knowing:
|
|
115
|
+
|
|
116
|
+
- **Keyword-style calls count as one positional hash.** `opts(a: 1)` is a call
|
|
117
|
+
with one argument, because overload blocks cannot declare keywords.
|
|
118
|
+
- **Blocks passed to an overloaded method are not forwarded** to the implementation.
|
|
119
|
+
- **`return` inside an overload block raises `LocalJumpError`.** Use `next value`
|
|
120
|
+
(or just make the value the last expression).
|
|
121
|
+
- **`super` inside an overload block is not supported.**
|
|
122
|
+
- **Overloading replaces an existing method of the same name.** Define normal
|
|
123
|
+
methods under different names. A later plain `def` with the same name replaces
|
|
124
|
+
the dispatcher, as ordinary Ruby redefinition does.
|
|
125
|
+
- **Inheritance:** subclass instances can call overloads defined on a parent class.
|
|
126
|
+
Declaring `overload` for the same name in a subclass starts a new set of
|
|
127
|
+
overloads that shadows the parent's rather than extending it.
|
|
128
|
+
|
|
129
|
+
## Development
|
|
130
|
+
|
|
131
|
+
```sh
|
|
132
|
+
bundle install
|
|
133
|
+
bundle exec rake # runs the RSpec suite
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Design notes
|
|
137
|
+
|
|
138
|
+
- `Registry` holds one method's implementations, keyed by argument count, and
|
|
139
|
+
rejects duplicates and unsupported signatures.
|
|
140
|
+
- `Dispatcher` installs the public method with `define_method` and routes each
|
|
141
|
+
call with `instance_exec`.
|
|
142
|
+
- The argument count is read by defining the block as a method on a throwaway
|
|
143
|
+
class, because `Proc#arity` is unreliable for blocks (`proc { |a = 1| }.arity`
|
|
144
|
+
is `0`, and plain procs report required parameters as `:opt`).
|
|
145
|
+
|
|
146
|
+
## License
|
|
147
|
+
|
|
148
|
+
MIT
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RbFunctionOverload
|
|
4
|
+
# Installs the public method that routes calls to the right implementation.
|
|
5
|
+
module Dispatcher
|
|
6
|
+
# Defines +registry.name+ on +klass+. The registry is captured by the
|
|
7
|
+
# closure (rather than looked up via self.class), so inherited calls keep
|
|
8
|
+
# working in subclasses.
|
|
9
|
+
def self.install(klass, registry)
|
|
10
|
+
klass.define_method(registry.name) do |*args|
|
|
11
|
+
Dispatcher.dispatch(self, registry, args)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Runs the implementation matching args.size with +receiver+ as self.
|
|
16
|
+
def self.dispatch(receiver, registry, args)
|
|
17
|
+
implementation = registry.fetch(args.size)
|
|
18
|
+
raise ArgumentError, no_match_message(registry, args.size) unless implementation
|
|
19
|
+
|
|
20
|
+
receiver.instance_exec(*args, &implementation)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.no_match_message(registry, given)
|
|
24
|
+
"wrong number of arguments for overloaded method `#{registry.name}' " \
|
|
25
|
+
"(given #{given}, expected #{to_sentence(registry.arities)})"
|
|
26
|
+
end
|
|
27
|
+
private_class_method :no_match_message
|
|
28
|
+
|
|
29
|
+
def self.to_sentence(numbers)
|
|
30
|
+
return numbers.first.to_s if numbers.size == 1
|
|
31
|
+
|
|
32
|
+
"#{numbers[0..-2].join(', ')} or #{numbers.last}"
|
|
33
|
+
end
|
|
34
|
+
private_class_method :to_sentence
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RbFunctionOverload
|
|
4
|
+
# Holds the implementations of ONE overloaded method, indexed by the number
|
|
5
|
+
# of positional arguments each implementation takes.
|
|
6
|
+
class Registry
|
|
7
|
+
attr_reader :name
|
|
8
|
+
|
|
9
|
+
def initialize(name)
|
|
10
|
+
@name = name
|
|
11
|
+
@implementations = {}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Files +block+ under its positional argument count and returns that count.
|
|
15
|
+
# Raises before storing anything if the block is missing, has an
|
|
16
|
+
# unsupported signature, or duplicates an existing arity.
|
|
17
|
+
def register(block)
|
|
18
|
+
raise InvalidOverloadError, "overload :#{name} requires a block" if block.nil?
|
|
19
|
+
|
|
20
|
+
arity = positional_arity(block)
|
|
21
|
+
if @implementations.key?(arity)
|
|
22
|
+
raise DuplicateOverloadError,
|
|
23
|
+
"overload :#{name} with #{arity} #{arity == 1 ? 'argument' : 'arguments'} " \
|
|
24
|
+
"is already defined"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
@implementations[arity] = block
|
|
28
|
+
arity
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def fetch(arity)
|
|
32
|
+
@implementations[arity]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Sorted list of the argument counts that have an implementation.
|
|
36
|
+
def arities
|
|
37
|
+
@implementations.keys.sort
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
# Proc#arity and Proc#parameters are unreliable for plain blocks:
|
|
43
|
+
# `proc { |a = 1| }.arity` is 0 and `proc { |a| }.parameters` reports :opt.
|
|
44
|
+
# Defining the block as a method gives it method semantics, so required
|
|
45
|
+
# parameters are reported as :req and optional ones as :opt.
|
|
46
|
+
def positional_arity(block)
|
|
47
|
+
parameters = method_parameters(block)
|
|
48
|
+
unsupported = parameters.map(&:first).uniq - [:req]
|
|
49
|
+
unless unsupported.empty?
|
|
50
|
+
raise InvalidOverloadError,
|
|
51
|
+
"overload :#{name} only supports plain required positional parameters " \
|
|
52
|
+
"(unsupported parameter kinds: #{unsupported.join(', ')})"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
parameters.size
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def method_parameters(block)
|
|
59
|
+
Class.new { define_method(:probe, &block) }.instance_method(:probe).parameters
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "rb_function_overload/version"
|
|
4
|
+
require_relative "rb_function_overload/registry"
|
|
5
|
+
require_relative "rb_function_overload/dispatcher"
|
|
6
|
+
|
|
7
|
+
# Adds argument-count-based method overloading to a class:
|
|
8
|
+
#
|
|
9
|
+
# class Calculator
|
|
10
|
+
# include RbFunctionOverload
|
|
11
|
+
#
|
|
12
|
+
# overload(:add) { |a| a }
|
|
13
|
+
# overload(:add) { |a, b| a + b }
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# Calculator.new.add(1) # => 1
|
|
17
|
+
# Calculator.new.add(1, 2) # => 3
|
|
18
|
+
module RbFunctionOverload
|
|
19
|
+
class Error < StandardError; end
|
|
20
|
+
|
|
21
|
+
# The same method name was overloaded twice with the same argument count.
|
|
22
|
+
class DuplicateOverloadError < Error; end
|
|
23
|
+
|
|
24
|
+
# The overload declaration itself is unusable (no block, or a signature
|
|
25
|
+
# outside the scope of v0.1).
|
|
26
|
+
class InvalidOverloadError < Error; end
|
|
27
|
+
|
|
28
|
+
def self.included(base)
|
|
29
|
+
base.extend(ClassMethods)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
module ClassMethods
|
|
33
|
+
# Declares one implementation of +name+. The argument count is taken from
|
|
34
|
+
# the block's parameters. Returns +name+ as a Symbol, like `def` does.
|
|
35
|
+
def overload(name, &block)
|
|
36
|
+
name = name.to_sym
|
|
37
|
+
registry = overload_registries[name] || Registry.new(name)
|
|
38
|
+
|
|
39
|
+
# Validates and stores the block; raises before anything is installed.
|
|
40
|
+
registry.register(block)
|
|
41
|
+
|
|
42
|
+
unless overload_registries.key?(name)
|
|
43
|
+
overload_registries[name] = registry
|
|
44
|
+
Dispatcher.install(self, registry)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
name
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def overload_registries
|
|
53
|
+
@overload_registries ||= {}
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rb_function_overload
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ishaan Bhela
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rake
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '13.0'
|
|
19
|
+
type: :development
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '13.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rspec
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '3.12'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '3.12'
|
|
40
|
+
description: Declare multiple implementations of the same instance method and dispatch
|
|
41
|
+
between them by the number of positional arguments.
|
|
42
|
+
email:
|
|
43
|
+
- ishaan@example.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- LICENSE
|
|
49
|
+
- README.md
|
|
50
|
+
- lib/rb_function_overload.rb
|
|
51
|
+
- lib/rb_function_overload/dispatcher.rb
|
|
52
|
+
- lib/rb_function_overload/registry.rb
|
|
53
|
+
- lib/rb_function_overload/version.rb
|
|
54
|
+
homepage: https://github.com/Ishaan453/rb-function-overload
|
|
55
|
+
licenses:
|
|
56
|
+
- MIT
|
|
57
|
+
metadata:
|
|
58
|
+
homepage_uri: https://github.com/Ishaan453/rb-function-overload
|
|
59
|
+
source_code_uri: https://github.com/Ishaan453/rb-function-overload
|
|
60
|
+
rubygems_mfa_required: 'true'
|
|
61
|
+
rdoc_options: []
|
|
62
|
+
require_paths:
|
|
63
|
+
- lib
|
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.0'
|
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - ">="
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: '0'
|
|
74
|
+
requirements: []
|
|
75
|
+
rubygems_version: 3.6.9
|
|
76
|
+
specification_version: 4
|
|
77
|
+
summary: Argument-count-based method overloading for Ruby.
|
|
78
|
+
test_files: []
|