我正在將應用程式從 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 Docs 中沒有看到任何表明從 2.6 開始的變化
2.6
打開(檔案名,模式=“rb”,**選項){|faster_csv| ... }
3.0
打開(檔案路徑,模式=“rb”,**選項){|csv| ... } → 物件
我看到 CSV 庫在 Ruby 3.0 中進行了更新,但沒有看到可能導致此代碼不再作業的更改。
任何提示將非常感謝。
愛德華
uj5u.com熱心網友回復:
在 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/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/431900.html
