首页 > 解决方案 > 需要“时间”后的很多警告

问题描述

当我导入 Time 和 HTTParty 时,我收到了以下警告:

C:/Ruby27-x64/lib/ruby/2.7.0/time.rb:34: warning: already initialized constant Class::ZoneOffset
C:/Ruby27-x64/lib/ruby/2.7.0/Time.rb:34: warning: previous definition of ZoneOffset was here
C:/Ruby27-x64/lib/ruby/2.7.0/time.rb:137: warning: already initialized constant Class::LeapYearMonthDays
C:/Ruby27-x64/lib/ruby/2.7.0/Time.rb:137: warning: previous definition of LeapYearMonthDays was here
C:/Ruby27-x64/lib/ruby/2.7.0/time.rb:138: warning: already initialized constant Class::CommonYearMonthDays
C:/Ruby27-x64/lib/ruby/2.7.0/Time.rb:138: warning: previous definition of CommonYearMonthDays was here
C:/Ruby27-x64/lib/ruby/2.7.0/time.rb:475: warning: already initialized constant Class::MonthValue
C:/Ruby27-x64/lib/ruby/2.7.0/Time.rb:475: warning: previous definition of MonthValue was here
C:/Ruby27-x64/lib/ruby/2.7.0/time.rb:677: warning: already initialized constant Time::RFC2822_DAY_NAME
C:/Ruby27-x64/lib/ruby/2.7.0/Time.rb:677: warning: previous definition of RFC2822_DAY_NAME was here
C:/Ruby27-x64/lib/ruby/2.7.0/time.rb:681: warning: already initialized constant Time::RFC2822_MONTH_NAME
C:/Ruby27-x64/lib/ruby/2.7.0/Time.rb:681: warning: previous definition of RFC2822_MONTH_NAME was here

这实际上是所有代码:

require 'HTTParty'
require 'Time'

有人知道我如何解决这个问题吗?

标签: rubytimetimestamprubygemshttparty

解决方案


Ruby中没有Time库。但是,有一个time图书馆。

require 'Time'看起来您正在使用不区分大小写的文件系统,所以当您Time.rb使用time.rb. (操作系统会对TIME.RBortImE.rBTiMe.Rbor 说同样的话……)

因此,Ruby 将加载Time.rb(实际上是time.rb)。但是,在内部,该time库当然会在require 'time'任何地方使用。现在,Ruby 会检测到文件何时已经被加载,并且会忽略它,但是 Time.rbtime.rb是两个不同的文件名,所以 Ruby 会自然地同时加载它们。

但是,由于它们是同一个文件,因此其中的所有内容都time.rb将执行两次,这意味着您将收到对该文件中每个常量定义每个方法定义的警告。

解决方案很简单:使用require 'time',因为这是库的入口文件的名称。

另一种方法是使用区分大小写的文件系统,在这种情况下,您只会得到一个LoadError异常,告诉您没有名为Time.rb.


推荐阅读