很多警告要求“时间”

时间:2020-09-12 04:30:49

标签: ruby time timestamp rubygems httparty

当我导入时间和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'

有人知道我该如何解决?

1 个答案:

答案 0 :(得分:1)

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

您似乎使用的是不区分大小写的文件系统,因此当您require 'Time'时,操作系统将“撒谎”到Ruby并告诉它Time.rb实际上存在,即使存在实际上只有time.rb。 (操作系统将对TIME.RBtImE.rBTiMe.Rb或…说同样的话)

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

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

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

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