Rails 3 ruby​​ 1.9.2 CSV“已初始化常量..”警告

发布于 2024-11-05 19:03:18 字数 931 浏览 0 评论 0原文

我想将读取/写入数据到 CSV 文件的功能添加到我的模型之一。在 1.9 之前的 ruby​​ 版本中,这可以通过 fastCSV 完成,但现在这是 ruby​​ 的一部分。我有以下设置:

#in my_model.rb
require 'CSV'

class MyModel < ActiveRecord::Base
   ... stuff ...
   def self.dump_to_csv(file=File.join(Rails.root, 'tmp', 'dump', 'my_model.csv'))
      CSV.open(file, "w") do |csv|
         keys = new.attributes.keys
         csv << keys
         all.each do |m|
            csv << m.attributes.values_at(*keys)
         end
      end
    end
end

这工作正常,但是当我运行测试时,我收到了一系列以下形式的警告

/Users/x/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/csv.rb:201: warning: already initialized constant VERSION
/Users/x/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/csv.rb:863: warning: already initialized constant FieldInfo
/Users/x/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/csv.rb:866: warning: already initialized constant DateMatcher
... 

:如何删除这些警告?

I want to add the ability to read/write data to a CSV file to one of my models. In versions of ruby prior to 1.9 this would have been done with fasterCSV, but this is now part of ruby. I have the following setup:

#in my_model.rb
require 'CSV'

class MyModel < ActiveRecord::Base
   ... stuff ...
   def self.dump_to_csv(file=File.join(Rails.root, 'tmp', 'dump', 'my_model.csv'))
      CSV.open(file, "w") do |csv|
         keys = new.attributes.keys
         csv << keys
         all.each do |m|
            csv << m.attributes.values_at(*keys)
         end
      end
    end
end

This works fine, however when I come to run tests I get a load of warnings of the form

/Users/x/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/csv.rb:201: warning: already initialized constant VERSION
/Users/x/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/csv.rb:863: warning: already initialized constant FieldInfo
/Users/x/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/csv.rb:866: warning: already initialized constant DateMatcher
... 

How can I remove these warnings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情仇皆在手 2024-11-12 19:03:18

我遇到了同样的问题,经过一番挖掘,我意识到我需要“CSV”,而我应该要求“csv”,它应该全部小写。

I ran into the same issue, after some digging I realized that I was requiring 'CSV' where I should have been requiring 'csv' it should be all lowercase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文