ruby - 需要“时间”后的很多警告
问题描述
当我导入 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'
有人知道我如何解决这个问题吗?
解决方案
Ruby中没有Time
库。但是,有一个time
图书馆。
require 'Time'
看起来您正在使用不区分大小写的文件系统,所以当您Time.rb
使用time.rb
. (操作系统会对TIME.RB
ortImE.rB
或TiMe.Rb
or 说同样的话……)
因此,Ruby 将加载Time.rb
(实际上是time.rb
)。但是,在内部,该time
库当然会在require 'time'
任何地方使用。现在,Ruby 会检测到文件何时已经被加载,并且会忽略它,但是 Time.rb
和time.rb
是两个不同的文件名,所以 Ruby 会自然地同时加载它们。
但是,由于它们是同一个文件,因此其中的所有内容都time.rb
将执行两次,这意味着您将收到对该文件中每个常量定义和每个方法定义的警告。
解决方案很简单:使用require 'time'
,因为这是库的入口文件的名称。
另一种方法是使用区分大小写的文件系统,在这种情况下,您只会得到一个LoadError
异常,告诉您没有名为Time.rb
.
推荐阅读
- maven - 如何使用 JDK 11 构建 JDK7 pom.xml?
- javascript - jQuery .Click 事件触发器超出了它分配给的输入元素
- react-native - 如何以编程方式调用 Pressable 的 onPress() 方法?
- postgresql - PostgreSQL:to_tsquery 以搜索开始并以搜索结束
- python - 由 Python 解释的 C++ 字节数组(结构)
- javascript - Plyr - 持续时间结束时监听事件
- python - 提取文本直到达到第一个数字 - Python
- php - PHP联系页面设置
- javascript - 是否可以从网站运行不和谐的机器人命令
- javascript - 如何使用 Node js 在单个 API 调用中传递两个请求 - Cloudant