rbs 0.11.0 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Rakefile +9 -4
- data/Steepfile +28 -0
- data/bin/steep +4 -0
- data/bin/test_runner.rb +10 -5
- data/lib/rbs/ast/comment.rb +7 -1
- data/lib/rbs/ast/declarations.rb +15 -9
- data/lib/rbs/buffer.rb +1 -1
- data/lib/rbs/definition.rb +22 -13
- data/lib/rbs/definition_builder.rb +79 -55
- data/lib/rbs/environment.rb +24 -10
- data/lib/rbs/location.rb +1 -5
- data/lib/rbs/method_type.rb +5 -5
- data/lib/rbs/namespace.rb +14 -3
- data/lib/rbs/parser.y +0 -8
- data/lib/rbs/prototype/rb.rb +3 -4
- data/lib/rbs/prototype/rbi.rb +1 -2
- data/lib/rbs/substitution.rb +4 -3
- data/lib/rbs/type_name.rb +18 -1
- data/lib/rbs/type_name_resolver.rb +10 -3
- data/lib/rbs/types.rb +27 -21
- data/lib/rbs/variance_calculator.rb +8 -5
- data/lib/rbs/version.rb +1 -1
- data/sig/annotation.rbs +26 -0
- data/sig/buffer.rbs +28 -0
- data/sig/builtin_names.rbs +41 -0
- data/sig/comment.rbs +26 -0
- data/sig/declarations.rbs +202 -0
- data/sig/definition.rbs +129 -0
- data/sig/definition_builder.rbs +95 -0
- data/sig/environment.rbs +94 -0
- data/sig/environment_loader.rbs +4 -0
- data/sig/location.rbs +52 -0
- data/sig/members.rbs +160 -0
- data/sig/method_types.rbs +40 -0
- data/sig/namespace.rbs +124 -0
- data/sig/polyfill.rbs +3 -0
- data/sig/rbs.rbs +3 -0
- data/sig/substitution.rbs +39 -0
- data/sig/type_name_resolver.rbs +24 -0
- data/sig/typename.rbs +70 -0
- data/sig/types.rbs +361 -0
- data/sig/util.rbs +13 -0
- data/sig/variance_calculator.rbs +35 -0
- data/stdlib/bigdecimal/big_decimal.rbs +887 -0
- data/stdlib/bigdecimal/math/big_math.rbs +142 -0
- data/stdlib/builtin/builtin.rbs +0 -3
- data/stdlib/builtin/math.rbs +26 -26
- data/stdlib/builtin/struct.rbs +9 -10
- data/stdlib/forwardable/forwardable.rbs +204 -0
- data/stdlib/set/set.rbs +1 -1
- data/stdlib/uri/file.rbs +167 -0
- data/stdlib/uri/generic.rbs +875 -0
- data/steep/Gemfile +3 -0
- data/steep/Gemfile.lock +55 -0
- metadata +36 -6
@@ -0,0 +1,142 @@
|
|
1
|
+
# Provides mathematical functions.
|
2
|
+
#
|
3
|
+
# Example:
|
4
|
+
#
|
5
|
+
# require "bigdecimal/math"
|
6
|
+
#
|
7
|
+
# include BigMath
|
8
|
+
#
|
9
|
+
# a = BigDecimal((PI(100)/2).to_s)
|
10
|
+
# puts sin(a,100) # => 0.99999999999999999999......e0
|
11
|
+
#
|
12
|
+
module BigMath
|
13
|
+
# Computes e (the base of natural logarithms) to the specified number of digits
|
14
|
+
# of precision, `numeric`.
|
15
|
+
#
|
16
|
+
# BigMath.E(10).to_s
|
17
|
+
# #=> "0.271828182845904523536028752390026306410273e1"
|
18
|
+
#
|
19
|
+
def self.E: (Numeric prec) -> BigDecimal
|
20
|
+
|
21
|
+
# Computes the value of pi to the specified number of digits of precision,
|
22
|
+
# `numeric`.
|
23
|
+
#
|
24
|
+
# BigMath.PI(10).to_s
|
25
|
+
# #=> "0.3141592653589793238462643388813853786957412e1"
|
26
|
+
#
|
27
|
+
def self.PI: (Numeric prec) -> BigDecimal
|
28
|
+
|
29
|
+
# Computes the arctangent of `decimal` to the specified number of digits of
|
30
|
+
# precision, `numeric`.
|
31
|
+
#
|
32
|
+
# If `decimal` is NaN, returns NaN.
|
33
|
+
#
|
34
|
+
# BigMath.atan(BigDecimal('-1'), 16).to_s
|
35
|
+
# #=> "-0.785398163397448309615660845819878471907514682065e0"
|
36
|
+
#
|
37
|
+
def self.atan: (BigDecimal x, Numeric prec) -> BigDecimal
|
38
|
+
|
39
|
+
# Computes the cosine of `decimal` to the specified number of digits of
|
40
|
+
# precision, `numeric`.
|
41
|
+
#
|
42
|
+
# If `decimal` is Infinity or NaN, returns NaN.
|
43
|
+
#
|
44
|
+
# BigMath.cos(BigMath.PI(4), 16).to_s
|
45
|
+
# #=> "-0.999999999999999999999999999999856613163740061349e0"
|
46
|
+
#
|
47
|
+
def self.cos: (BigDecimal x, Numeric prec) -> BigDecimal
|
48
|
+
|
49
|
+
# Computes the value of e (the base of natural logarithms) raised to the power
|
50
|
+
# of `decimal`, to the specified number of digits of precision.
|
51
|
+
#
|
52
|
+
# If `decimal` is infinity, returns Infinity.
|
53
|
+
#
|
54
|
+
# If `decimal` is NaN, returns NaN.
|
55
|
+
#
|
56
|
+
def self.exp: (BigDecimal, Numeric prec) -> BigDecimal
|
57
|
+
|
58
|
+
# Computes the natural logarithm of `decimal` to the specified number of digits
|
59
|
+
# of precision, `numeric`.
|
60
|
+
#
|
61
|
+
# If `decimal` is zero or negative, raises Math::DomainError.
|
62
|
+
#
|
63
|
+
# If `decimal` is positive infinity, returns Infinity.
|
64
|
+
#
|
65
|
+
# If `decimal` is NaN, returns NaN.
|
66
|
+
#
|
67
|
+
def self.log: (BigDecimal, Numeric prec) -> BigDecimal
|
68
|
+
|
69
|
+
# Computes the sine of `decimal` to the specified number of digits of precision,
|
70
|
+
# `numeric`.
|
71
|
+
#
|
72
|
+
# If `decimal` is Infinity or NaN, returns NaN.
|
73
|
+
#
|
74
|
+
# BigMath.sin(BigMath.PI(5)/4, 5).to_s
|
75
|
+
# #=> "0.70710678118654752440082036563292800375e0"
|
76
|
+
#
|
77
|
+
def self.sin: (BigDecimal x, Numeric prec) -> BigDecimal
|
78
|
+
|
79
|
+
# Computes the square root of `decimal` to the specified number of digits of
|
80
|
+
# precision, `numeric`.
|
81
|
+
#
|
82
|
+
# BigMath.sqrt(BigDecimal('2'), 16).to_s
|
83
|
+
# #=> "0.1414213562373095048801688724e1"
|
84
|
+
#
|
85
|
+
def self.sqrt: (BigDecimal x, Numeric prec) -> BigDecimal
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
# Computes e (the base of natural logarithms) to the specified number of digits
|
90
|
+
# of precision, `numeric`.
|
91
|
+
#
|
92
|
+
# BigMath.E(10).to_s
|
93
|
+
# #=> "0.271828182845904523536028752390026306410273e1"
|
94
|
+
#
|
95
|
+
def E: (Numeric prec) -> BigDecimal
|
96
|
+
|
97
|
+
# Computes the value of pi to the specified number of digits of precision,
|
98
|
+
# `numeric`.
|
99
|
+
#
|
100
|
+
# BigMath.PI(10).to_s
|
101
|
+
# #=> "0.3141592653589793238462643388813853786957412e1"
|
102
|
+
#
|
103
|
+
def PI: (Numeric prec) -> BigDecimal
|
104
|
+
|
105
|
+
# Computes the arctangent of `decimal` to the specified number of digits of
|
106
|
+
# precision, `numeric`.
|
107
|
+
#
|
108
|
+
# If `decimal` is NaN, returns NaN.
|
109
|
+
#
|
110
|
+
# BigMath.atan(BigDecimal('-1'), 16).to_s
|
111
|
+
# #=> "-0.785398163397448309615660845819878471907514682065e0"
|
112
|
+
#
|
113
|
+
def atan: (BigDecimal x, Numeric prec) -> BigDecimal
|
114
|
+
|
115
|
+
# Computes the cosine of `decimal` to the specified number of digits of
|
116
|
+
# precision, `numeric`.
|
117
|
+
#
|
118
|
+
# If `decimal` is Infinity or NaN, returns NaN.
|
119
|
+
#
|
120
|
+
# BigMath.cos(BigMath.PI(4), 16).to_s
|
121
|
+
# #=> "-0.999999999999999999999999999999856613163740061349e0"
|
122
|
+
#
|
123
|
+
def cos: (BigDecimal x, Numeric prec) -> BigDecimal
|
124
|
+
|
125
|
+
# Computes the sine of `decimal` to the specified number of digits of precision,
|
126
|
+
# `numeric`.
|
127
|
+
#
|
128
|
+
# If `decimal` is Infinity or NaN, returns NaN.
|
129
|
+
#
|
130
|
+
# BigMath.sin(BigMath.PI(5)/4, 5).to_s
|
131
|
+
# #=> "0.70710678118654752440082036563292800375e0"
|
132
|
+
#
|
133
|
+
def sin: (BigDecimal x, Numeric prec) -> BigDecimal
|
134
|
+
|
135
|
+
# Computes the square root of `decimal` to the specified number of digits of
|
136
|
+
# precision, `numeric`.
|
137
|
+
#
|
138
|
+
# BigMath.sqrt(BigDecimal('2'), 16).to_s
|
139
|
+
# #=> "0.1414213562373095048801688724e1"
|
140
|
+
#
|
141
|
+
def sqrt: (BigDecimal x, Numeric prec) -> BigDecimal
|
142
|
+
end
|
data/stdlib/builtin/builtin.rbs
CHANGED
data/stdlib/builtin/math.rbs
CHANGED
@@ -13,7 +13,7 @@ module Math
|
|
13
13
|
#
|
14
14
|
# Math.acos(0) == Math::PI/2 #=> true
|
15
15
|
#
|
16
|
-
def self.acos: (
|
16
|
+
def self.acos: (Numeric x) -> Float
|
17
17
|
|
18
18
|
# Computes the inverse hyperbolic cosine of `x`.
|
19
19
|
#
|
@@ -23,7 +23,7 @@ module Math
|
|
23
23
|
#
|
24
24
|
# Math.acosh(1) #=> 0.0
|
25
25
|
#
|
26
|
-
def self.acosh: (
|
26
|
+
def self.acosh: (Numeric x) -> Float
|
27
27
|
|
28
28
|
# Computes the arc sine of `x`. Returns -PI/2..PI/2.
|
29
29
|
#
|
@@ -33,7 +33,7 @@ module Math
|
|
33
33
|
#
|
34
34
|
# Math.asin(1) == Math::PI/2 #=> true
|
35
35
|
#
|
36
|
-
def self.asin: (
|
36
|
+
def self.asin: (Numeric x) -> Float
|
37
37
|
|
38
38
|
# Computes the inverse hyperbolic sine of `x`.
|
39
39
|
#
|
@@ -43,7 +43,7 @@ module Math
|
|
43
43
|
#
|
44
44
|
# Math.asinh(1) #=> 0.881373587019543
|
45
45
|
#
|
46
|
-
def self.asinh: (
|
46
|
+
def self.asinh: (Numeric x) -> Float
|
47
47
|
|
48
48
|
# Computes the arc tangent of `x`. Returns -PI/2..PI/2.
|
49
49
|
#
|
@@ -53,7 +53,7 @@ module Math
|
|
53
53
|
#
|
54
54
|
# Math.atan(0) #=> 0.0
|
55
55
|
#
|
56
|
-
def self.atan: (
|
56
|
+
def self.atan: (Numeric x) -> Float
|
57
57
|
|
58
58
|
# Computes the arc tangent given `y` and `x`. Returns a Float in the range
|
59
59
|
# -PI..PI. Return value is a angle in radians between the positive x-axis of
|
@@ -78,7 +78,7 @@ module Math
|
|
78
78
|
# Math.atan2(-INFINITY, INFINITY) #=> -0.7853981633974483
|
79
79
|
# Math.atan2(-INFINITY, -INFINITY) #=> -2.356194490192345
|
80
80
|
#
|
81
|
-
def self.atan2: (
|
81
|
+
def self.atan2: (Numeric y, Numeric x) -> Float
|
82
82
|
|
83
83
|
# Computes the inverse hyperbolic tangent of `x`.
|
84
84
|
#
|
@@ -88,7 +88,7 @@ module Math
|
|
88
88
|
#
|
89
89
|
# Math.atanh(1) #=> Infinity
|
90
90
|
#
|
91
|
-
def self.atanh: (
|
91
|
+
def self.atanh: (Numeric x) -> Float
|
92
92
|
|
93
93
|
# Returns the cube root of `x`.
|
94
94
|
#
|
@@ -119,7 +119,7 @@ module Math
|
|
119
119
|
# # [8, 2.0, 8.0]
|
120
120
|
# # [9, 2.0800838230519, 9.0]
|
121
121
|
#
|
122
|
-
def self.cbrt: (
|
122
|
+
def self.cbrt: (Numeric x) -> Float
|
123
123
|
|
124
124
|
# Computes the cosine of `x` (expressed in radians). Returns a Float in the
|
125
125
|
# range -1.0..1.0.
|
@@ -130,7 +130,7 @@ module Math
|
|
130
130
|
#
|
131
131
|
# Math.cos(Math::PI) #=> -1.0
|
132
132
|
#
|
133
|
-
def self.cos: (
|
133
|
+
def self.cos: (Numeric x) -> Float
|
134
134
|
|
135
135
|
# Computes the hyperbolic cosine of `x` (expressed in radians).
|
136
136
|
#
|
@@ -140,7 +140,7 @@ module Math
|
|
140
140
|
#
|
141
141
|
# Math.cosh(0) #=> 1.0
|
142
142
|
#
|
143
|
-
def self.cosh: (
|
143
|
+
def self.cosh: (Numeric x) -> Float
|
144
144
|
|
145
145
|
# Calculates the error function of `x`.
|
146
146
|
#
|
@@ -150,7 +150,7 @@ module Math
|
|
150
150
|
#
|
151
151
|
# Math.erf(0) #=> 0.0
|
152
152
|
#
|
153
|
-
def self.erf: (
|
153
|
+
def self.erf: (Numeric x) -> Float
|
154
154
|
|
155
155
|
# Calculates the complementary error function of x.
|
156
156
|
#
|
@@ -160,7 +160,7 @@ module Math
|
|
160
160
|
#
|
161
161
|
# Math.erfc(0) #=> 1.0
|
162
162
|
#
|
163
|
-
def self.erfc: (
|
163
|
+
def self.erfc: (Numeric x) -> Float
|
164
164
|
|
165
165
|
# Returns e**x.
|
166
166
|
#
|
@@ -172,7 +172,7 @@ module Math
|
|
172
172
|
# Math.exp(1) #=> 2.718281828459045
|
173
173
|
# Math.exp(1.5) #=> 4.4816890703380645
|
174
174
|
#
|
175
|
-
def self.exp: (
|
175
|
+
def self.exp: (Numeric x) -> Float
|
176
176
|
|
177
177
|
# Returns a two-element array containing the normalized fraction (a Float) and
|
178
178
|
# exponent (an Integer) of `x`.
|
@@ -180,7 +180,7 @@ module Math
|
|
180
180
|
# fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11]
|
181
181
|
# fraction * 2**exponent #=> 1234.0
|
182
182
|
#
|
183
|
-
def self.frexp: (
|
183
|
+
def self.frexp: (Numeric x) -> [ Float, Integer ]
|
184
184
|
|
185
185
|
# Calculates the gamma function of x.
|
186
186
|
#
|
@@ -216,21 +216,21 @@ module Math
|
|
216
216
|
# # [25, 6.204484017332391e+23, 620448401733239439360000]
|
217
217
|
# # [26, 1.5511210043330954e+25, 15511210043330985984000000]
|
218
218
|
#
|
219
|
-
def self.gamma: (
|
219
|
+
def self.gamma: (Numeric x) -> Float
|
220
220
|
|
221
221
|
# Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle with
|
222
222
|
# sides `x` and `y`.
|
223
223
|
#
|
224
224
|
# Math.hypot(3, 4) #=> 5.0
|
225
225
|
#
|
226
|
-
def self.hypot: (
|
226
|
+
def self.hypot: (Numeric x, Numeric y) -> Float
|
227
227
|
|
228
228
|
# Returns the value of `fraction`*(2**`exponent`).
|
229
229
|
#
|
230
230
|
# fraction, exponent = Math.frexp(1234)
|
231
231
|
# Math.ldexp(fraction, exponent) #=> 1234.0
|
232
232
|
#
|
233
|
-
def self.ldexp: (
|
233
|
+
def self.ldexp: (Numeric fraction, Numeric exponent) -> Float
|
234
234
|
|
235
235
|
# Calculates the logarithmic gamma of `x` and the sign of gamma of `x`.
|
236
236
|
#
|
@@ -241,9 +241,9 @@ module Math
|
|
241
241
|
#
|
242
242
|
# Math.lgamma(0) #=> [Infinity, 1]
|
243
243
|
#
|
244
|
-
def self.lgamma: (
|
244
|
+
def self.lgamma: (Numeric x) -> [ Float, Integer ]
|
245
245
|
|
246
|
-
def self.log: (
|
246
|
+
def self.log: (Numeric x, ?Numeric base) -> Float
|
247
247
|
|
248
248
|
# Returns the base 10 logarithm of `x`.
|
249
249
|
#
|
@@ -255,7 +255,7 @@ module Math
|
|
255
255
|
# Math.log10(10) #=> 1.0
|
256
256
|
# Math.log10(10**100) #=> 100.0
|
257
257
|
#
|
258
|
-
def self.log10: (
|
258
|
+
def self.log10: (Numeric x) -> Float
|
259
259
|
|
260
260
|
# Returns the base 2 logarithm of `x`.
|
261
261
|
#
|
@@ -268,7 +268,7 @@ module Math
|
|
268
268
|
# Math.log2(32768) #=> 15.0
|
269
269
|
# Math.log2(65536) #=> 16.0
|
270
270
|
#
|
271
|
-
def self.log2: (
|
271
|
+
def self.log2: (Numeric x) -> Float
|
272
272
|
|
273
273
|
# Computes the sine of `x` (expressed in radians). Returns a Float in the range
|
274
274
|
# -1.0..1.0.
|
@@ -279,7 +279,7 @@ module Math
|
|
279
279
|
#
|
280
280
|
# Math.sin(Math::PI/2) #=> 1.0
|
281
281
|
#
|
282
|
-
def self.sin: (
|
282
|
+
def self.sin: (Numeric x) -> Float
|
283
283
|
|
284
284
|
# Computes the hyperbolic sine of `x` (expressed in radians).
|
285
285
|
#
|
@@ -289,7 +289,7 @@ module Math
|
|
289
289
|
#
|
290
290
|
# Math.sinh(0) #=> 0.0
|
291
291
|
#
|
292
|
-
def self.sinh: (
|
292
|
+
def self.sinh: (Numeric x) -> Float
|
293
293
|
|
294
294
|
# Returns the non-negative square root of `x`.
|
295
295
|
#
|
@@ -319,7 +319,7 @@ module Math
|
|
319
319
|
#
|
320
320
|
# See also BigDecimal#sqrt and Integer.sqrt.
|
321
321
|
#
|
322
|
-
def self.sqrt: (
|
322
|
+
def self.sqrt: (Numeric x) -> Float
|
323
323
|
|
324
324
|
# Computes the tangent of `x` (expressed in radians).
|
325
325
|
#
|
@@ -329,7 +329,7 @@ module Math
|
|
329
329
|
#
|
330
330
|
# Math.tan(0) #=> 0.0
|
331
331
|
#
|
332
|
-
def self.tan: (
|
332
|
+
def self.tan: (Numeric x) -> Float
|
333
333
|
|
334
334
|
# Computes the hyperbolic tangent of `x` (expressed in radians).
|
335
335
|
#
|
@@ -339,7 +339,7 @@ module Math
|
|
339
339
|
#
|
340
340
|
# Math.tanh(0) #=> 0.0
|
341
341
|
#
|
342
|
-
def self.tanh: (
|
342
|
+
def self.tanh: (Numeric x) -> Float
|
343
343
|
end
|
344
344
|
|
345
345
|
# Definition of the mathematical constant E for Euler's number (e) as a Float
|
data/stdlib/builtin/struct.rbs
CHANGED
@@ -1,40 +1,39 @@
|
|
1
1
|
# A [Struct](Struct) is a convenient way to bundle a
|
2
2
|
# number of attributes together, using accessor methods, without having to
|
3
3
|
# write an explicit class.
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# The [Struct](Struct) class generates new subclasses
|
6
6
|
# that hold a set of members and their values. For each member a reader
|
7
7
|
# and writer method is created similar to
|
8
8
|
# [Module\#attr\_accessor](https://ruby-doc.org/core-2.6.3/Module.html#method-i-attr_accessor)
|
9
9
|
# .
|
10
|
-
#
|
10
|
+
#
|
11
11
|
# ```ruby
|
12
12
|
# Customer = Struct.new(:name, :address) do
|
13
13
|
# def greeting
|
14
14
|
# "Hello #{name}!"
|
15
15
|
# end
|
16
16
|
# end
|
17
|
-
#
|
17
|
+
#
|
18
18
|
# dave = Customer.new("Dave", "123 Main")
|
19
19
|
# dave.name #=> "Dave"
|
20
20
|
# dave.greeting #=> "Hello Dave!"
|
21
21
|
# ```
|
22
|
-
#
|
22
|
+
#
|
23
23
|
# See [::new](Struct#method-c-new) for further
|
24
24
|
# examples of creating struct subclasses and instances.
|
25
|
-
#
|
25
|
+
#
|
26
26
|
# In the method descriptions that follow, a "member" parameter refers to a
|
27
27
|
# struct member which is either a quoted string ( `"name"` ) or a
|
28
28
|
# [Symbol](https://ruby-doc.org/core-2.6.3/Symbol.html) ( `:name` ).
|
29
29
|
class Struct[Elem] < Object
|
30
30
|
include Enumerable[Elem, Struct[Elem]]
|
31
31
|
|
32
|
-
|
32
|
+
type attribute_name = Symbol | String
|
33
33
|
|
34
|
-
def
|
35
|
-
| () -> self
|
34
|
+
def initialize: (attribute_name, *attribute_name, ?keyword_init: bool) ?{ () -> void } -> void
|
36
35
|
|
37
|
-
def
|
36
|
+
def each: () { (Elem) -> untyped } -> untyped
|
38
37
|
|
39
|
-
def
|
38
|
+
def self.members: () -> ::Array[Symbol]
|
40
39
|
end
|
@@ -0,0 +1,204 @@
|
|
1
|
+
# The Forwardable module provides delegation of specified methods to a
|
2
|
+
# designated object, using the methods #def_delegator and #def_delegators.
|
3
|
+
#
|
4
|
+
# For example, say you have a class RecordCollection which contains an array
|
5
|
+
# `@records`. You could provide the lookup method #record_number(), which
|
6
|
+
# simply calls #[] on the `@records` array, like this:
|
7
|
+
#
|
8
|
+
# require 'forwardable'
|
9
|
+
#
|
10
|
+
# class RecordCollection
|
11
|
+
# attr_accessor :records
|
12
|
+
# extend Forwardable
|
13
|
+
# def_delegator :@records, :[], :record_number
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# We can use the lookup method like so:
|
17
|
+
#
|
18
|
+
# r = RecordCollection.new
|
19
|
+
# r.records = [4,5,6]
|
20
|
+
# r.record_number(0) # => 4
|
21
|
+
#
|
22
|
+
# Further, if you wish to provide the methods #size, #<<, and #map, all of which
|
23
|
+
# delegate to @records, this is how you can do it:
|
24
|
+
#
|
25
|
+
# class RecordCollection # re-open RecordCollection class
|
26
|
+
# def_delegators :@records, :size, :<<, :map
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# r = RecordCollection.new
|
30
|
+
# r.records = [1,2,3]
|
31
|
+
# r.record_number(0) # => 1
|
32
|
+
# r.size # => 3
|
33
|
+
# r << 4 # => [1, 2, 3, 4]
|
34
|
+
# r.map { |x| x * 2 } # => [2, 4, 6, 8]
|
35
|
+
#
|
36
|
+
# You can even extend regular objects with Forwardable.
|
37
|
+
#
|
38
|
+
# my_hash = Hash.new
|
39
|
+
# my_hash.extend Forwardable # prepare object for delegation
|
40
|
+
# my_hash.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
|
41
|
+
# my_hash.puts "Howdy!"
|
42
|
+
#
|
43
|
+
# ## Another example
|
44
|
+
#
|
45
|
+
# You could use Forwardable as an alternative to inheritance, when you don't
|
46
|
+
# want to inherit all methods from the superclass. For instance, here is how you
|
47
|
+
# might add a range of `Array` instance methods to a new class `Queue`:
|
48
|
+
#
|
49
|
+
# class Queue
|
50
|
+
# extend Forwardable
|
51
|
+
#
|
52
|
+
# def initialize
|
53
|
+
# @q = [ ] # prepare delegate object
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# # setup preferred interface, enq() and deq()...
|
57
|
+
# def_delegator :@q, :push, :enq
|
58
|
+
# def_delegator :@q, :shift, :deq
|
59
|
+
#
|
60
|
+
# # support some general Array methods that fit Queues well
|
61
|
+
# def_delegators :@q, :clear, :first, :push, :shift, :size
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# q = Queue.new
|
65
|
+
# q.enq 1, 2, 3, 4, 5
|
66
|
+
# q.push 6
|
67
|
+
#
|
68
|
+
# q.shift # => 1
|
69
|
+
# while q.size > 0
|
70
|
+
# puts q.deq
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# q.enq "Ruby", "Perl", "Python"
|
74
|
+
# puts q.first
|
75
|
+
# q.clear
|
76
|
+
# puts q.first
|
77
|
+
#
|
78
|
+
# This should output:
|
79
|
+
#
|
80
|
+
# 2
|
81
|
+
# 3
|
82
|
+
# 4
|
83
|
+
# 5
|
84
|
+
# 6
|
85
|
+
# Ruby
|
86
|
+
# nil
|
87
|
+
#
|
88
|
+
# ## Notes
|
89
|
+
#
|
90
|
+
# Be advised, RDoc will not detect delegated methods.
|
91
|
+
#
|
92
|
+
# `forwardable.rb` provides single-method delegation via the def_delegator and
|
93
|
+
# def_delegators methods. For full-class delegation via DelegateClass, see
|
94
|
+
# `delegate.rb`.
|
95
|
+
#
|
96
|
+
module Forwardable
|
97
|
+
VERSION: String
|
98
|
+
|
99
|
+
FORWARDABLE_VERSION: String
|
100
|
+
|
101
|
+
# Takes a hash as its argument. The key is a symbol or an array of symbols.
|
102
|
+
# These symbols correspond to method names, instance variable names, or constant
|
103
|
+
# names (see def_delegator). The value is the accessor to which the methods
|
104
|
+
# will be delegated.
|
105
|
+
def instance_delegate: (Hash[Symbol | Array[Symbol], Symbol] hash) -> void
|
106
|
+
|
107
|
+
alias delegate instance_delegate
|
108
|
+
|
109
|
+
# Shortcut for defining multiple delegator methods, but with no provision for
|
110
|
+
# using a different name. The following two code samples have the same effect:
|
111
|
+
#
|
112
|
+
# def_delegators :@records, :size, :<<, :map
|
113
|
+
#
|
114
|
+
# def_delegator :@records, :size
|
115
|
+
# def_delegator :@records, :<<
|
116
|
+
# def_delegator :@records, :map
|
117
|
+
#
|
118
|
+
def def_instance_delegators: (Symbol | String accessor, *Symbol methods) -> void
|
119
|
+
|
120
|
+
alias def_delegators def_instance_delegators
|
121
|
+
|
122
|
+
# Define `method` as delegator instance method with an optional alias name
|
123
|
+
# `ali`. Method calls to `ali` will be delegated to `accessor.method`.
|
124
|
+
# `accessor` should be a method name, instance variable name, or constant name.
|
125
|
+
# Use the full path to the constant if providing the constant name. Returns the
|
126
|
+
# name of the method defined.
|
127
|
+
#
|
128
|
+
# class MyQueue
|
129
|
+
# CONST = 1
|
130
|
+
# extend Forwardable
|
131
|
+
# attr_reader :queue
|
132
|
+
# def initialize
|
133
|
+
# @queue = []
|
134
|
+
# end
|
135
|
+
#
|
136
|
+
# def_delegator :@queue, :push, :mypush
|
137
|
+
# def_delegator 'MyQueue::CONST', :to_i
|
138
|
+
# end
|
139
|
+
#
|
140
|
+
# q = MyQueue.new
|
141
|
+
# q.mypush 42
|
142
|
+
# q.queue #=> [42]
|
143
|
+
# q.push 23 #=> NoMethodError
|
144
|
+
# q.to_i #=> 1
|
145
|
+
#
|
146
|
+
def def_instance_delegator: (Symbol | String accessor, Symbol method, ?Symbol ali) -> void
|
147
|
+
|
148
|
+
alias def_delegator def_instance_delegator
|
149
|
+
end
|
150
|
+
|
151
|
+
# SingleForwardable can be used to setup delegation at the object level as well.
|
152
|
+
#
|
153
|
+
# printer = String.new
|
154
|
+
# printer.extend SingleForwardable # prepare object for delegation
|
155
|
+
# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
|
156
|
+
# printer.puts "Howdy!"
|
157
|
+
#
|
158
|
+
# Also, SingleForwardable can be used to set up delegation for a Class or
|
159
|
+
# Module.
|
160
|
+
#
|
161
|
+
# class Implementation
|
162
|
+
# def self.service
|
163
|
+
# puts "serviced!"
|
164
|
+
# end
|
165
|
+
# end
|
166
|
+
#
|
167
|
+
# module Facade
|
168
|
+
# extend SingleForwardable
|
169
|
+
# def_delegator :Implementation, :service
|
170
|
+
# end
|
171
|
+
#
|
172
|
+
# Facade.service #=> serviced!
|
173
|
+
#
|
174
|
+
# If you want to use both Forwardable and SingleForwardable, you can use methods
|
175
|
+
# def_instance_delegator and def_single_delegator, etc.
|
176
|
+
#
|
177
|
+
module SingleForwardable
|
178
|
+
# Takes a hash as its argument. The key is a symbol or an array of symbols.
|
179
|
+
# These symbols correspond to method names. The value is the accessor to which
|
180
|
+
# the methods will be delegated.
|
181
|
+
def single_delegate: (Hash[Symbol | Array[Symbol], Symbol] hash) -> void
|
182
|
+
|
183
|
+
alias delegate single_delegate
|
184
|
+
|
185
|
+
# Shortcut for defining multiple delegator methods, but with no provision for
|
186
|
+
# using a different name. The following two code samples have the same effect:
|
187
|
+
#
|
188
|
+
# def_delegators :@records, :size, :<<, :map
|
189
|
+
#
|
190
|
+
# def_delegator :@records, :size
|
191
|
+
# def_delegator :@records, :<<
|
192
|
+
# def_delegator :@records, :map
|
193
|
+
#
|
194
|
+
def def_single_delegators: (Symbol | String accessor, *Symbol methods) -> void
|
195
|
+
|
196
|
+
alias def_delegators def_single_delegators
|
197
|
+
|
198
|
+
# Defines a method *method* which delegates to *accessor* (i.e. it calls the
|
199
|
+
# method of the same name in *accessor*). If *new_name* is provided, it is used
|
200
|
+
# as the name for the delegate method. Returns the name of the method defined.
|
201
|
+
def def_single_delegator: (Symbol | String accessor, Symbol method, ?Symbol ali) -> void
|
202
|
+
|
203
|
+
alias def_delegator def_single_delegator
|
204
|
+
end
|