Rails 3 ruby 1.9.2 CSV“已初始化常量..”警告
我想将读取/写入数据到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,经过一番挖掘,我意识到我需要“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.