Ruby CSV 打开 - Ruby 3.0 中的参数数量错误

Ruby CSV Open - Wrong number of arguments in 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

open( filename, mode = "rb", **options ) { |faster_csv| ... }

3.0

open(file_path, mode = "rb", **options ) { |csv| ... } → object

我看到 CSV 库已在 Ruby 3.0 中更新,但我没有看到会导致此代码不再工作的更改。

如有任何提示,我们将不胜感激。

爱德华

在 Ruby 3.0 中,位置参数和关键字参数是分开的,在 Ruby 2.7 中已弃用。这意味着 Ruby 2.7 弃用警告:Using the last argument as keyword parameters is deprecated, 即。不再支持使用散列作为 **options 的参数。

您必须将其命名为:

# Manual spreading hash
CSV.open(csv_path, "wb", **{:col_sep => ";"}) do |csv|
  ...
end

# Or as Keyword argument 

CSV.open(csv_path, "wb", :col_sep => ";") do |csv|
  ...
end

编辑:另见 https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/