rbs 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/Rakefile +9 -4
  4. data/Steepfile +28 -0
  5. data/bin/steep +4 -0
  6. data/bin/test_runner.rb +10 -5
  7. data/lib/rbs/ast/comment.rb +7 -1
  8. data/lib/rbs/ast/declarations.rb +15 -9
  9. data/lib/rbs/buffer.rb +1 -1
  10. data/lib/rbs/definition.rb +22 -13
  11. data/lib/rbs/definition_builder.rb +79 -55
  12. data/lib/rbs/environment.rb +24 -10
  13. data/lib/rbs/location.rb +1 -5
  14. data/lib/rbs/method_type.rb +5 -5
  15. data/lib/rbs/namespace.rb +14 -3
  16. data/lib/rbs/parser.y +0 -8
  17. data/lib/rbs/prototype/rb.rb +3 -4
  18. data/lib/rbs/prototype/rbi.rb +1 -2
  19. data/lib/rbs/substitution.rb +4 -3
  20. data/lib/rbs/type_name.rb +18 -1
  21. data/lib/rbs/type_name_resolver.rb +10 -3
  22. data/lib/rbs/types.rb +27 -21
  23. data/lib/rbs/variance_calculator.rb +8 -5
  24. data/lib/rbs/version.rb +1 -1
  25. data/sig/annotation.rbs +26 -0
  26. data/sig/buffer.rbs +28 -0
  27. data/sig/builtin_names.rbs +41 -0
  28. data/sig/comment.rbs +26 -0
  29. data/sig/declarations.rbs +202 -0
  30. data/sig/definition.rbs +129 -0
  31. data/sig/definition_builder.rbs +95 -0
  32. data/sig/environment.rbs +94 -0
  33. data/sig/environment_loader.rbs +4 -0
  34. data/sig/location.rbs +52 -0
  35. data/sig/members.rbs +160 -0
  36. data/sig/method_types.rbs +40 -0
  37. data/sig/namespace.rbs +124 -0
  38. data/sig/polyfill.rbs +3 -0
  39. data/sig/rbs.rbs +3 -0
  40. data/sig/substitution.rbs +39 -0
  41. data/sig/type_name_resolver.rbs +24 -0
  42. data/sig/typename.rbs +70 -0
  43. data/sig/types.rbs +361 -0
  44. data/sig/util.rbs +13 -0
  45. data/sig/variance_calculator.rbs +35 -0
  46. data/stdlib/bigdecimal/big_decimal.rbs +887 -0
  47. data/stdlib/bigdecimal/math/big_math.rbs +142 -0
  48. data/stdlib/builtin/builtin.rbs +0 -3
  49. data/stdlib/builtin/math.rbs +26 -26
  50. data/stdlib/builtin/struct.rbs +9 -10
  51. data/stdlib/forwardable/forwardable.rbs +204 -0
  52. data/stdlib/set/set.rbs +1 -1
  53. data/stdlib/uri/file.rbs +167 -0
  54. data/stdlib/uri/generic.rbs +875 -0
  55. data/steep/Gemfile +3 -0
  56. data/steep/Gemfile.lock +55 -0
  57. 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
@@ -35,9 +35,6 @@ interface _Exception
35
35
  | (String arg0) -> Exception
36
36
  end
37
37
 
38
- class BigDecimal
39
- end
40
-
41
38
  type int = Integer | _ToInt
42
39
  type real = Integer | Float | Rational
43
40
 
@@ -13,7 +13,7 @@ module Math
13
13
  #
14
14
  # Math.acos(0) == Math::PI/2 #=> true
15
15
  #
16
- def self.acos: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal y, Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> [ Float, Integer ]
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x, Integer | Float | Rational | BigDecimal y) -> Float
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: (Integer | Float | Rational | BigDecimal fraction, Integer | Float | Rational | BigDecimal exponent) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> [ Float, Integer ]
244
+ def self.lgamma: (Numeric x) -> [ Float, Integer ]
245
245
 
246
- def self.log: (Integer | Float | Rational | BigDecimal x, ?Integer | Float | Rational | BigDecimal base) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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: (Integer | Float | Rational | BigDecimal x) -> Float
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
@@ -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
- def initialize: (Symbol | String arg0, *Symbol | String arg1, ?keyword_init: bool keyword_init) -> void
32
+ type attribute_name = Symbol | String
33
33
 
34
- def each: () { (Elem arg0) -> untyped } -> untyped
35
- | () -> self
34
+ def initialize: (attribute_name, *attribute_name, ?keyword_init: bool) ?{ () -> void } -> void
36
35
 
37
- def self.members: () -> ::Array[Symbol]
36
+ def each: () { (Elem) -> untyped } -> untyped
38
37
 
39
- def new: (*untyped args) -> Struct[untyped]
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