rbs 1.7.0.beta.4 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/CHANGELOG.md +58 -2
  4. data/Steepfile +0 -1
  5. data/core/array.rbs +3 -3
  6. data/core/binding.rbs +2 -0
  7. data/core/builtin.rbs +4 -0
  8. data/core/complex.rbs +0 -2
  9. data/core/enumerable.rbs +3 -3
  10. data/core/env.rbs +881 -0
  11. data/core/false_class.rbs +2 -0
  12. data/core/float.rbs +0 -2
  13. data/core/integer.rbs +0 -2
  14. data/core/nil_class.rbs +2 -0
  15. data/core/numeric.rbs +7 -0
  16. data/core/object.rbs +1 -1
  17. data/core/proc.rbs +2 -0
  18. data/core/rational.rbs +0 -2
  19. data/core/symbol.rbs +2 -0
  20. data/core/thread.rbs +1 -1
  21. data/core/true_class.rbs +2 -0
  22. data/core/unbound_method.rbs +13 -0
  23. data/docs/rbs_by_example.md +2 -2
  24. data/docs/syntax.md +25 -23
  25. data/ext/rbs_extension/parser.c +99 -95
  26. data/ext/rbs_extension/parserstate.c +0 -1
  27. data/ext/rbs_extension/rbs_extension.h +1 -1
  28. data/ext/rbs_extension/ruby_objs.c +8 -6
  29. data/ext/rbs_extension/ruby_objs.h +2 -4
  30. data/lib/rbs/ast/declarations.rb +6 -2
  31. data/lib/rbs/cli.rb +1 -1
  32. data/lib/rbs/collection/sources/git.rb +6 -1
  33. data/lib/rbs/definition_builder.rb +29 -2
  34. data/lib/rbs/environment.rb +1 -0
  35. data/lib/rbs/environment_walker.rb +4 -1
  36. data/lib/rbs/errors.rb +12 -0
  37. data/lib/rbs/prototype/helpers.rb +113 -0
  38. data/lib/rbs/prototype/rb.rb +2 -105
  39. data/lib/rbs/prototype/runtime.rb +16 -0
  40. data/lib/rbs/test/setup.rb +1 -0
  41. data/lib/rbs/type_alias_regularity.rb +115 -0
  42. data/lib/rbs/types.rb +11 -23
  43. data/lib/rbs/validator.rb +40 -7
  44. data/lib/rbs/variance_calculator.rb +52 -24
  45. data/lib/rbs/version.rb +1 -1
  46. data/lib/rbs/writer.rb +1 -1
  47. data/lib/rbs.rb +2 -0
  48. data/schema/decls.json +13 -1
  49. data/schema/types.json +8 -2
  50. data/sig/collection/collections.rbs +2 -0
  51. data/sig/declarations.rbs +9 -6
  52. data/sig/definition_builder.rbs +29 -0
  53. data/sig/environment_walker.rbs +26 -0
  54. data/sig/errors.rbs +10 -0
  55. data/sig/type_alias_regularity.rbs +92 -0
  56. data/sig/types.rbs +11 -8
  57. data/sig/validator.rbs +7 -0
  58. data/sig/variance_calculator.rbs +50 -0
  59. data/stdlib/bigdecimal/0/big_decimal.rbs +44 -0
  60. data/stdlib/csv/0/csv.rbs +49 -3
  61. data/stdlib/date/0/date.rbs +2 -2
  62. data/stdlib/set/0/set.rbs +3 -3
  63. data/steep/Gemfile.lock +10 -10
  64. metadata +8 -6
  65. data/lib/rbs/parser.y +0 -1805
  66. data/lib/ruby/signature.rb +0 -7
@@ -54,6 +54,21 @@ module RBS
54
54
  false
55
55
  end
56
56
  end
57
+
58
+ def incompatible?(params)
59
+ # @type set: Hash[Symbol]
60
+ set = Set[]
61
+
62
+ params.each do |param|
63
+ unless compatible?(param.name, with_annotation: param.variance)
64
+ set << param.name
65
+ end
66
+ end
67
+
68
+ unless set.empty?
69
+ set
70
+ end
71
+ end
57
72
  end
58
73
 
59
74
  attr_reader :builder
@@ -69,19 +84,12 @@ module RBS
69
84
  def in_method_type(method_type:, variables:)
70
85
  result = Result.new(variables: variables)
71
86
 
72
- method_type.type.each_param do |param|
73
- type(param.type, result: result, context: :contravariant)
74
- end
87
+ function(method_type.type, result: result, context: :covariant)
75
88
 
76
89
  if block = method_type.block
77
- block.type.each_param do |param|
78
- type(param.type, result: result, context: :covariant)
79
- end
80
- type(block.type.return_type, result: result, context: :contravariant)
90
+ function(block.type, result: result, context: :contravariant)
81
91
  end
82
92
 
83
- type(method_type.type.return_type, result: result, context: :covariant)
84
-
85
93
  result
86
94
  end
87
95
 
@@ -97,6 +105,14 @@ module RBS
97
105
  end
98
106
  end
99
107
 
108
+ def in_type_alias(name:)
109
+ decl = env.alias_decls[name].decl or raise
110
+ variables = decl.type_params.each.map(&:name)
111
+ Result.new(variables: variables).tap do |result|
112
+ type(decl.type, result: result, context: :covariant)
113
+ end
114
+ end
115
+
100
116
  def type(type, result:, context:)
101
117
  case type
102
118
  when Types::Variable
@@ -110,7 +126,7 @@ module RBS
110
126
  result.invariant(type.name)
111
127
  end
112
128
  end
113
- when Types::ClassInstance, Types::Interface
129
+ when Types::ClassInstance, Types::Interface, Types::Alias
114
130
  NoTypeFoundError.check!(type.name,
115
131
  env: env,
116
132
  location: type.location)
@@ -120,6 +136,8 @@ module RBS
120
136
  env.class_decls[type.name].type_params
121
137
  when Types::Interface
122
138
  env.interface_decls[type.name].decl.type_params
139
+ when Types::Alias
140
+ env.alias_decls[type.name].decl.type_params
123
141
  end
124
142
 
125
143
  type.args.each.with_index do |ty, i|
@@ -130,26 +148,36 @@ module RBS
130
148
  when :covariant
131
149
  type(ty, result: result, context: context)
132
150
  when :contravariant
133
- # @type var con: variance
134
- con = case context
135
- when :invariant
136
- :invariant
137
- when :covariant
138
- :contravariant
139
- when :contravariant
140
- :covariant
141
- else
142
- raise
143
- end
144
- type(ty, result: result, context: con)
151
+ type(ty, result: result, context: negate(context))
145
152
  end
146
153
  end
147
- when Types::Tuple, Types::Record, Types::Union, Types::Intersection
148
- # Covariant types
154
+ when Types::Proc
155
+ function(type.type, result: result, context: context)
156
+ else
149
157
  type.each_type do |ty|
150
158
  type(ty, result: result, context: context)
151
159
  end
152
160
  end
153
161
  end
162
+
163
+ def function(type, result:, context:)
164
+ type.each_param do |param|
165
+ type(param.type, result: result, context: negate(context))
166
+ end
167
+ type(type.return_type, result: result, context: context)
168
+ end
169
+
170
+ def negate(variance)
171
+ case variance
172
+ when :invariant
173
+ :invariant
174
+ when :covariant
175
+ :contravariant
176
+ when :contravariant
177
+ :covariant
178
+ else
179
+ raise
180
+ end
181
+ end
154
182
  end
155
183
  end
data/lib/rbs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RBS
2
- VERSION = "1.7.0.beta.4"
2
+ VERSION = "1.8.0"
3
3
  end
data/lib/rbs/writer.rb CHANGED
@@ -119,7 +119,7 @@ module RBS
119
119
  when AST::Declarations::Alias
120
120
  write_comment decl.comment
121
121
  write_annotation decl.annotations
122
- puts "type #{decl.name} = #{decl.type}"
122
+ puts "type #{name_and_params(decl.name, decl.type_params)} = #{decl.type}"
123
123
 
124
124
  when AST::Declarations::Interface
125
125
  write_comment decl.comment
data/lib/rbs.rb CHANGED
@@ -32,6 +32,7 @@ require "rbs/constant"
32
32
  require "rbs/constant_table"
33
33
  require "rbs/ast/comment"
34
34
  require "rbs/writer"
35
+ require "rbs/prototype/helpers"
35
36
  require "rbs/prototype/rbi"
36
37
  require "rbs/prototype/rb"
37
38
  require "rbs/prototype/runtime"
@@ -44,6 +45,7 @@ require "rbs/repository"
44
45
  require "rbs/ancestor_graph"
45
46
  require "rbs/locator"
46
47
  require "rbs/type_alias_dependency"
48
+ require "rbs/type_alias_regularity"
47
49
  require "rbs/collection"
48
50
 
49
51
  require "rbs_extension"
data/schema/decls.json CHANGED
@@ -12,6 +12,18 @@
12
12
  "name": {
13
13
  "type": "string"
14
14
  },
15
+ "type_params": {
16
+ "type": "object",
17
+ "properties": {
18
+ "params": {
19
+ "type": "array",
20
+ "items": {
21
+ "$ref": "#/definitions/moduleTypeParam"
22
+ }
23
+ }
24
+ },
25
+ "required": ["params"]
26
+ },
15
27
  "type": {
16
28
  "$ref": "types.json"
17
29
  },
@@ -28,7 +40,7 @@
28
40
  "$ref": "comment.json"
29
41
  }
30
42
  },
31
- "required": ["declaration", "name", "type", "annotations", "location", "comment"]
43
+ "required": ["declaration", "name", "type_params", "type", "annotations", "location", "comment"]
32
44
  },
33
45
  "constant": {
34
46
  "title": "Constant declaration: `VERSION: String`, ...",
data/schema/types.json CHANGED
@@ -106,7 +106,7 @@
106
106
  "required": ["class", "name", "args", "location"]
107
107
  },
108
108
  "alias": {
109
- "title": "Type alias: `u`, `ty`, `json`, ...",
109
+ "title": "Type alias: `u`, `ty`, `json`, `list[Integer]`, ...",
110
110
  "type": "object",
111
111
  "properties": {
112
112
  "class": {
@@ -116,11 +116,17 @@
116
116
  "name": {
117
117
  "type": "string"
118
118
  },
119
+ "args": {
120
+ "type": "array",
121
+ "items": {
122
+ "$ref": "#"
123
+ }
124
+ },
119
125
  "location": {
120
126
  "$ref": "location.json"
121
127
  }
122
128
  },
123
- "required": ["class", "name", "location"]
129
+ "required": ["class", "name", "args", "location"]
124
130
  },
125
131
  "tuple": {
126
132
  "title": "Tuple type: `[Foo, bar]`, ...",
@@ -46,6 +46,8 @@ module RBS
46
46
 
47
47
  def _install: (dest: Pathname , config_entry: Config::gem_entry) -> void
48
48
 
49
+ def cp_r: (Pathname, Pathname) -> void
50
+
49
51
  def setup!: (revision: String) -> void
50
52
 
51
53
  def need_to_fetch?: (String revision ) -> bool
data/sig/declarations.rbs CHANGED
@@ -217,19 +217,22 @@ module RBS
217
217
  end
218
218
 
219
219
  class Alias < Base
220
- # type loc = Location
221
- # ^^^^ keyword
222
- # ^^^ name
223
- # ^ eq
224
- type loc = Location[:keyword | :name | :eq, bot]
220
+ # type loc[T] = Location[T, bot]
221
+ # ^^^^ keyword
222
+ # ^^^ name
223
+ # ^^^ type_params
224
+ # ^ eq
225
+ #
226
+ type loc = Location[:keyword | :name | :eq, :type_params]
225
227
 
226
228
  attr_reader name: TypeName
229
+ attr_reader type_params: ModuleTypeParams
227
230
  attr_reader type: Types::t
228
231
  attr_reader annotations: Array[Annotation]
229
232
  attr_reader location: loc?
230
233
  attr_reader comment: Comment?
231
234
 
232
- def initialize: (name: TypeName, type: Types::t, annotations: Array[Annotation], location: loc?, comment: Comment?) -> void
235
+ def initialize: (name: TypeName, type_params: ModuleTypeParams, type: Types::t, annotations: Array[Annotation], location: loc?, comment: Comment?) -> void
233
236
 
234
237
  include _HashEqual
235
238
  include _ToJson
@@ -43,8 +43,37 @@ module RBS
43
43
 
44
44
  def define_methods: (Definition, interface_methods: Hash[Symbol, Definition::Method], methods: MethodBuilder::Methods, super_interface_method: bool) -> void
45
45
 
46
+ # Expand a type alias of given name without type arguments.
47
+ # Raises an error if the type alias requires arguments.
48
+ #
49
+ # Assume `type foo[T] = [T, T]`:
50
+ #
51
+ # ```
52
+ # expand_alias("::foo") # => error
53
+ # ```
54
+ #
46
55
  def expand_alias: (TypeName) -> Types::t
47
56
 
57
+ # Expand a type alias of given name with arguments of `untyped`.
58
+ #
59
+ # Assume `type foo[T] = [T, T]`:
60
+ #
61
+ # ```
62
+ # expand_alias1("::foo") # => [untyped, untyped]
63
+ # ```
64
+ #
65
+ def expand_alias1: (TypeName) -> Types::t
66
+
67
+ # Expand a type alias of given name with `args`.
68
+ #
69
+ # Assume `type foo[T] = [T, T]`:
70
+ #
71
+ # ```
72
+ # expand_alias2("::foo", ["::Integer"]) # => [::Integer, ::Integer]
73
+ # ```
74
+ #
75
+ def expand_alias2: (TypeName, Array[Types::t] args) -> Types::t
76
+
48
77
  def update: (env: Environment, ancestor_builder: AncestorBuilder, except: _Each[TypeName]) -> DefinitionBuilder
49
78
  end
50
79
  end
@@ -1,4 +1,28 @@
1
1
  module RBS
2
+ # EnvironmentWalker provides topological sort of class/module definitions.
3
+ #
4
+ # If a method, attribute, or ancestor in a class definition have a reference to another class, it is dependency.
5
+ #
6
+ # ```rb
7
+ # walker = EnvironmentWalker.new(env: env)
8
+ #
9
+ # walker.each_strongly_connected_component do |scc|
10
+ # # Yields an array of strongly connected components.
11
+ # end
12
+ # ```
13
+ #
14
+ # The `#only_ancestors!` method limits the dependency only to ancestors.
15
+ # Only super classes and included modules are dependencies with the option.
16
+ # This is useful to calculate the dependencies of class hierarchy.
17
+ #
18
+ # ```rb
19
+ # walker = EnvironmentWalker.new(env: env).only_ancestors!
20
+ #
21
+ # walker.each_strongly_connected_component do |scc|
22
+ # # Yields an array of strongly connected components.
23
+ # end
24
+ # ```
25
+ #
2
26
  class EnvironmentWalker
3
27
  class InstanceNode
4
28
  attr_reader type_name: TypeName
@@ -32,6 +56,8 @@ module RBS
32
56
 
33
57
  def tsort_each_child: (node) { (node) -> void } -> void
34
58
 
59
+ private
60
+
35
61
  def each_type_name: (Types::t) { (TypeName) -> void } -> void
36
62
 
37
63
  def each_type_node: (Types::t) { (node) -> void } -> void
data/sig/errors.rbs CHANGED
@@ -220,4 +220,14 @@ module RBS
220
220
 
221
221
  def name: () -> String
222
222
  end
223
+
224
+ class NonregularTypeAliasError < LoadingError
225
+ # Diagnostic reported from `TypeAliasRegularity`.
226
+ attr_reader diagnostic: TypeAliasRegularity::Diagnostic
227
+
228
+ # Location of the definition.
229
+ attr_reader location: Location[untyped, untyped]?
230
+
231
+ def initialize: (diagnostic: TypeAliasRegularity::Diagnostic, location: Location[untyped, untyped]?) -> void
232
+ end
223
233
  end
@@ -0,0 +1,92 @@
1
+ module RBS
2
+ # `TypeAliasRegularity` validates if a type alias is regular or not.
3
+ #
4
+ # Generic and recursive type alias cannot be polymorphic in their definitions.
5
+ #
6
+ # ```rbs
7
+ # type foo[T] = Integer
8
+ # | foo[T]? # Allowed. The type argument of `foo` doesn't change.
9
+ #
10
+ # type bar[T] = Integer
11
+ # | foo[T]
12
+ # | foo[Array[T]] # Allowed. There are two type arguments `T` and `Array[T]` of `foo`, but it's not definition of `foo`.
13
+ #
14
+ # type baz[T] = Integer
15
+ # | baz[Array[T]] # Error. Recursive definition of `baz` has different type argument from the definition.
16
+ # ```
17
+ #
18
+ # The `#nonregular?` method can be used to test if given type name is regular or not.
19
+ #
20
+ # ```rb
21
+ # validator = RBS::TypeAliasRegularity.validate(env: env)
22
+ #
23
+ # validator.nonregular?(TypeName("::foo")) # => nil
24
+ # validator.nonregular?(TypeName("::bar")) # => nil
25
+ # validator.nonregular?(TypeName("::baz")) # => TypeAliasRegularity::Diagnostic
26
+ # ```
27
+ #
28
+ # A special case is when the type argument is `untyped`.
29
+ #
30
+ # ```rbs
31
+ # type foo[T] = Integer | foo[untyped] # This is allowed.
32
+ # ```
33
+ #
34
+ class TypeAliasRegularity
35
+ attr_reader env: Environment
36
+
37
+ attr_reader builder: DefinitionBuilder
38
+
39
+ attr_reader diagnostics: Hash[TypeName, Diagnostic]
40
+
41
+ # `Diagnostic` represents an non-regular type alias declaration error.
42
+ # It consists of the name of the alias type and a type on which the nonregularity is detected.
43
+ #
44
+ # ```rbs
45
+ # type t[T] = Integer | t[T?]
46
+ # ```
47
+ #
48
+ # The type `t` is nonregular because it contains `t[T?]` on it's right hand side.
49
+ #
50
+ # ```
51
+ # diagnostic = validator.nonregular?(TypeName("::t"))
52
+ # diagnostic.type_name # => TypeName("::t")
53
+ # diagnostic.nonregular_type # => t[T?]
54
+ # ```
55
+ #
56
+ class Diagnostic
57
+ attr_reader type_name: TypeName
58
+
59
+ attr_reader nonregular_type: Types::Alias
60
+
61
+ def initialize: (type_name: TypeName, nonregular_type: Types::Alias) -> void
62
+ end
63
+
64
+ # Returns new instance which already run `#validate`.
65
+ #
66
+ def self.validate: (env: Environment) -> TypeAliasRegularity
67
+
68
+ def initialize: (env: Environment) -> void
69
+
70
+ # Returns `Diagnostic` instance if the alias type is nonregular.
71
+ # Regurns `nil` if the alias type is regular.
72
+ #
73
+ def nonregular?: (TypeName) -> Diagnostic?
74
+
75
+ def validate: () -> void
76
+
77
+ private
78
+
79
+ def validate_alias_type: (Types::Alias, Set[TypeName], Hash[TypeName, Types::Alias]) -> void
80
+
81
+ # Returns alias type for given type name, if the alias is generic.
82
+ # Returns nil if the type alias is not generic.
83
+ #
84
+ def build_alias_type: (TypeName) -> Types::Alias?
85
+
86
+ def compatible_args?: (Array[Types::t], Array[Types::t]) -> boolish
87
+
88
+ def each_alias_type: (Types::t) { (Types::Alias) -> void } -> void
89
+
90
+ def each_mutual_alias_defs: () { (Set[TypeName]) -> void } -> void
91
+ end
92
+ end
data/sig/types.rbs CHANGED
@@ -227,18 +227,21 @@ module RBS
227
227
  end
228
228
 
229
229
  class Alias
230
- attr_reader name: TypeName
230
+ # foo
231
+ # ^^^ => name
232
+ #
233
+ # foo[bar, baz]
234
+ # ^^^ => name
235
+ # ^^^^^^^^^^ => args
236
+ #
237
+ type loc = Location[:name, :args]
231
238
 
232
- type loc = Location[bot, bot]
239
+ attr_reader location: loc?
233
240
 
234
- def initialize: (name: TypeName, location: loc?) -> void
241
+ def initialize: (name: TypeName, args: Array[t], location: loc?) -> void
235
242
 
236
243
  include _TypeBase
237
- include NoFreeVariables
238
- include NoSubst
239
- include EmptyEachType
240
-
241
- attr_reader location: loc?
244
+ include Application
242
245
  end
243
246
 
244
247
  class Tuple
data/sig/validator.rbs CHANGED
@@ -1,8 +1,15 @@
1
1
  module RBS
2
2
  class Validator
3
3
  attr_reader env: Environment
4
+
4
5
  attr_reader resolver: TypeNameResolver
5
6
 
7
+ attr_reader definition_builder: DefinitionBuilder
8
+
9
+ attr_reader type_alias_dependency: TypeAliasDependency
10
+
11
+ attr_reader type_alias_regularity: TypeAliasRegularity
12
+
6
13
  def initialize: (env: Environment, resolver: TypeNameResolver) -> void
7
14
 
8
15
  def absolute_type: (Types::t, context: TypeNameResolver::context) { (Types::t) -> TypeName } -> Types::t
@@ -1,7 +1,47 @@
1
1
  module RBS
2
+ # Calculate the use variances of type variables in declaration.
3
+ #
4
+ # ```rb
5
+ # calculator = VarianceCalculator.new(builder: builder)
6
+ #
7
+ # # Calculates variances in a method type
8
+ # result = calculator.in_method_type(method_type: method_type, variables: variables)
9
+ #
10
+ # # Calculates variances in a inheritance/mixin/...
11
+ # result = calculator.in_inherit(name: name, args: args, variables: variables)
12
+ #
13
+ # # Calculates variances in a type alias
14
+ # result = calculator.in_type_alias(name: name, args: args, variables: variables)
15
+ # ```
16
+ #
17
+ # See `RBS::VarianceCaluculator::Result` for information recorded in the `Result` object.
18
+ #
2
19
  class VarianceCalculator
3
20
  type variance = :unused | :covariant | :contravariant | :invariant
4
21
 
22
+ # Result contains the set of type variables and it's variance in a occurrence.
23
+ #
24
+ # ```rb
25
+ # # Enumerates recorded type variables
26
+ # result.each do |name, variance|
27
+ # # name is the name of a type variable
28
+ # # variance is one of :unused | :covariant | :contravariant | :invariant
29
+ # end
30
+ # ```
31
+ #
32
+ # You can test with `compatible?` method if the type variable occurrences are compatible with specified (annotated) variance.
33
+ #
34
+ # ```rb
35
+ # # When T is declared as `out T`
36
+ # result.compatible?(:T, with_annotation: :covariant)
37
+ #
38
+ # # When T is declared as `in T`
39
+ # result.compatible?(:T, with_annotation: :contravariant)
40
+ #
41
+ # # When T is declared as `T`
42
+ # result.compatible?(:T, with_annotation: :invariant)
43
+ # ```
44
+ #
5
45
  class Result
6
46
  attr_reader result: Hash[Symbol, variance]
7
47
 
@@ -18,6 +58,8 @@ module RBS
18
58
  def include?: (Symbol) -> bool
19
59
 
20
60
  def compatible?: (Symbol, with_annotation: variance) -> bool
61
+
62
+ def incompatible?: (AST::Declarations::ModuleTypeParams) -> Set[Symbol]?
21
63
  end
22
64
 
23
65
  attr_reader builder: DefinitionBuilder
@@ -30,6 +72,14 @@ module RBS
30
72
 
31
73
  def in_inherit: (name: TypeName, args: Array[Types::t], variables: Array[Symbol]) -> Result
32
74
 
75
+ def in_type_alias: (name: TypeName) -> Result
76
+
77
+ private
78
+
33
79
  def type: (Types::t, result: Result, context: variance) -> void
80
+
81
+ def function: (Types::Function, result: Result, context: variance) -> void
82
+
83
+ def negate: (variance) -> variance
34
84
  end
35
85
  end
@@ -885,3 +885,47 @@ BigDecimal::SIGN_POSITIVE_ZERO: Integer
885
885
  # The version of bigdecimal library
886
886
  #
887
887
  BigDecimal::VERSION: String
888
+
889
+ module Kernel
890
+ private
891
+
892
+ # Create a new BigDecimal object.
893
+ #
894
+ # initial
895
+ # : The initial value, as an Integer, a Float, a Rational, a BigDecimal, or a
896
+ # String.
897
+ #
898
+ # If it is a String, spaces are ignored and unrecognized characters
899
+ # terminate the value.
900
+ #
901
+ # digits
902
+ # : The number of significant digits, as an Integer. If omitted or 0, the
903
+ # number of significant digits is determined from the initial value.
904
+ #
905
+ # The actual number of significant digits used in computation is usually
906
+ # larger than the specified number.
907
+ #
908
+ # exception
909
+ # : Whether an exception should be raised on invalid arguments. `true` by
910
+ # default, if passed `false`, just returns `nil` for invalid.
911
+ #
912
+ #
913
+ # #### Exceptions
914
+ #
915
+ # TypeError
916
+ # : If the `initial` type is neither Integer, Float, Rational, nor BigDecimal,
917
+ # this exception is raised.
918
+ #
919
+ # TypeError
920
+ # : If the `digits` is not an Integer, this exception is raised.
921
+ #
922
+ # ArgumentError
923
+ # : If `initial` is a Float, and the `digits` is larger than Float::DIG + 1,
924
+ # this exception is raised.
925
+ #
926
+ # ArgumentError
927
+ # : If the `initial` is a Float or Rational, and the `digits` value is
928
+ # omitted, this exception is raised.
929
+ #
930
+ def self?.BigDecimal: ((real | string | BigDecimal) initial, ?int digits, ?exception: bool) -> BigDecimal
931
+ end
data/stdlib/csv/0/csv.rbs CHANGED
@@ -360,6 +360,49 @@ class CSV < Object
360
360
  # output non-ASCII compatible data.
361
361
  #
362
362
  def self.generate: (?String str, **untyped options) { (CSV csv) -> void } -> String
363
+
364
+ # :call-seq:
365
+ # csv.each -> enumerator
366
+ # csv.each {|row| ...}
367
+ #
368
+ # Calls the block with each successive row.
369
+ # The data source must be opened for reading.
370
+ #
371
+ # Without headers:
372
+ # string = "foo,0\nbar,1\nbaz,2\n"
373
+ # csv = CSV.new(string)
374
+ # csv.each do |row|
375
+ # p row
376
+ # end
377
+ # Output:
378
+ # ["foo", "0"]
379
+ # ["bar", "1"]
380
+ # ["baz", "2"]
381
+ #
382
+ # With headers:
383
+ # string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
384
+ # csv = CSV.new(string, headers: true)
385
+ # csv.each do |row|
386
+ # p row
387
+ # end
388
+ # Output:
389
+ # <CSV::Row "Name":"foo" "Value":"0">
390
+ # <CSV::Row "Name":"bar" "Value":"1">
391
+ # <CSV::Row "Name":"baz" "Value":"2">
392
+ #
393
+ # ---
394
+ #
395
+ # Raises an exception if the source is not opened for reading:
396
+ # string = "foo,0\nbar,1\nbaz,2\n"
397
+ # csv = CSV.new(string)
398
+ # csv.close
399
+ # # Raises IOError (not opened for reading)
400
+ # csv.each do |row|
401
+ # p row
402
+ # end
403
+ def each: () -> Enumerator[untyped, Integer]
404
+ | () { (untyped) -> void } -> Integer
405
+
363
406
  end
364
407
 
365
408
  # The options used when no overrides are given by calling code. They are:
@@ -408,7 +451,7 @@ CSV::VERSION: String
408
451
  # processing is activated.
409
452
  #
410
453
  class CSV::Row < Object
411
- include Enumerable[untyped]
454
+ include Enumerable[Array[String]]
412
455
  extend Forwardable
413
456
 
414
457
  # If a two-element Array is provided, it is assumed to be a header and field and
@@ -464,7 +507,8 @@ class CSV::Row < Object
464
507
  #
465
508
  # Support for Enumerable.
466
509
  #
467
- def each: () { (*untyped) -> untyped } -> untyped
510
+ def each: () -> Enumerator[Array[String], self]
511
+ | () { (Array[String]) -> void } -> self
468
512
 
469
513
  alias each_pair each
470
514
 
@@ -717,7 +761,9 @@ class CSV::Table[out Elem] < Object
717
761
  #
718
762
  # If no block is given, an Enumerator is returned.
719
763
  #
720
- def each: () { (*untyped) -> untyped } -> untyped
764
+ def each: () -> Enumerator[untyped, self]
765
+ | () { (untyped) -> void } -> self
766
+ | () { (*untyped) -> void } -> self
721
767
 
722
768
  def empty?: (*untyped args) { (*untyped) -> untyped } -> untyped
723
769