Ruby CSV Open - Ruby 3.0 中参数数量错误
我正在将应用程序从 Ruby 2.6 迁移到 Ruby 3.0,并且在打开 CSV 文件进行写入时遇到问题。
以下代码在 2.6 中运行良好。
CSV.open(csv_path, "wb", {:col_sep => ";"}) do |csv|
...
end
当我迁移到 3.0 时,我收到错误“参数数量错误(给定 3,预期 1..2)”。
我在 Ruby 文档中没有看到任何表明 2.6
2.6
发生变化的内容打开(文件名,模式=“rb”,**选项){|faster_csv| ... }
3.0
打开(文件路径,模式=“rb”,**选项){|csv| ... } → 对象
我看到 CSV 库已在 Ruby 3.0 中更新,但我没有看到可能发生的更改导致此代码不再工作。
任何提示将不胜感激。
爱德华
I am migrating an application from Ruby 2.6 to Ruby 3.0 and am running into an issue opening a CSV file for writing.
The following code worked fine in 2.6.
CSV.open(csv_path, "wb", {:col_sep => ";"}) do |csv|
...
end
When I move to 3.0 I am getting the error "wrong number of arguments (given 3, expected 1..2)".
I am not seeing anything in Ruby Docs to indicate a change from 2.6
2.6
open( filename, mode = "rb", **options ) { |faster_csv| ... }
3.0
open(file_path, mode = "rb", **options ) { |csv| ... } → object
I see that the CSV library was updated in Ruby 3.0, but am not seeing what could have changed that would cause this code to no longer work.
Any tips would be greatly appreciated.
Edward
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Ruby 3.0 中,位置参数和关键字参数是分开的,在 Ruby 2.7 中已弃用。这意味着 Ruby 2.7 弃用警告:
不推荐使用最后一个参数作为关键字参数,
即。不再支持使用哈希作为**options
的参数。您必须将其称为:
编辑:另请参阅 https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
In Ruby 3.0, positional arguments and keyword arguments was separated, with deprecation in Ruby 2.7. That means, that the Ruby 2.7 deprecation warning:
Using the last argument as keyword parameters is deprecated,
ie. using a hash as argument for**options
is no longer supported.You'll have to call it either as:
EDIT: See also https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/