Repository navigation
Update type for CSV.foreach - #1738
Conversation
| # before parsing. | ||
| # | ||
| def self.foreach: [U] (String | IO | StringIO path, ?::Hash[Symbol, U] options) { (::Array[String?] arg0) -> void } -> void | ||
| def self.foreach: (String | IO path, ?String mode, headers: true, **untyped options) { (::CSV::Row arg0) -> void } -> void |
There was a problem hiding this comment.
Accurately, the headers keyword argument can't be only received true, but also truthy value, for example, String and Symbol, etc.
However, As headers argument is often provided true, I defined true only in this methods.
Please let me know if not good. Thanks.
Because csv gem promoted bundled gem from default gem.
| ast (2.4.2) | ||
| base64 (0.2.0) | ||
| bigdecimal (3.1.6) | ||
| csv (3.2.8) |
There was a problem hiding this comment.
Add csv gem to Gemfile because it is changed from default gem to bundled gem in ruby 3.4.
ref: #1734
soutaro
left a comment
There was a problem hiding this comment.
Thanks!
Test is failing but unrelated to this change.
|
Technically, the type definition will result in an incorrect result: bool = true
CSV.foreach(..., headers: bool) do |row|
# `bool` is `bool` type, not `true`, and `row` is `String`.
endI think it's not too weird to assume we pass a |
See also: - ruby/rbs#2258 - ruby/rbs#2099 - ruby/rbs#2012 - ruby/rbs#2003 - ruby/rbs#1903 - ruby/rbs#1864 - ruby/rbs#1738 - ruby/rbs#241 Thank you: - @euglena1215 - @aki19035vc - @tk0miya - @m11o - @connorshea
This Pull Request updates the type signatures of
CSV.foreachin RBS.When CSV.foreach is called with the headers keyword argument, the block parameter or the elements of the returned Enumerator are of type
CSV::Row. If the headers argument is not provided, the elements of the Enumerator will be arrays with elements of type String.This change accurately reflects the behavior of CSV.foreach depending on the presence of the headers keyword argument.
This Pull Request does not include changes for other methods that accept the headers keyword argument and have behavior variations based on this argument. After this Pull Request is merged, I plan to address these methods in a separate Pull Request.
Thanks.
CSV.foreach: https://github.com/ruby/csv/blob/0cba3e766d34373043b4ca0bbae93c8de12014f0/lib/csv.rb#L1332