-1

When i import Time and HTTParty i got these warnings:

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

This is literally all the code:

require 'HTTParty'
require 'Time'

Somebody knows how i fix this?

André Kuljis
  • 90
  • 1
  • 9

1 Answers1

1

There is no Time library in Ruby. There is, however, a time library.

It looks like you are using a case-insensitive file system, so when you require 'Time', the Operating System will "lie" to Ruby and tell it that Time.rb actually exists, even though there is really only a time.rb. (The OS will say the same thing about TIME.RB or tImE.rB or TiMe.Rb or …)

Therefore, Ruby will load Time.rb (which is really time.rb). However, internally, the time library will of course use require 'time' everywhere. Now, Ruby detects when a file has already been loaded and will just ignore it, BUT Time.rb and time.rb are two different file names, so Ruby will naturally load them both.

Since they are the same file, though, everything in time.rb will get executed twice, which means that you will get a warning for every single constant definition and every single method definition in that file.

The solution is simple: use require 'time' because that's the name of the library's entry file.

The alternative would be to use a case-sensitive file system, in which case you would simply get a LoadError exception telling you that there is no file named Time.rb.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • Oh i get you, thanks. Does this means that all library/gem i'm gonna use should be lowercase? How can i use a case-sensitive file system? – André Kuljis Sep 12 '20 at 19:37
  • 1
    You should use the *actual* filename. Most case-insensitive file systems are *case-preserving*, i.e. even though they don't care about the case, they will still store the case. So, if you look at the filename, you can see what the *actual* case of the filename is. For many libraries, the documentation will also have an example in the documentation what the correct argument to `require` is. There are also some community coding standards, such as the name of the file should always be the `lower_snake_case` version of the main class or module in the file. But that is only a convention, it is not – Jörg W Mittag Sep 12 '20 at 20:28
  • 1
    … enforced, and not all projects follow it. For example, following that rule, the main library file for `HTTParty` should be named `h_t_t_party.rb` or maybe `http_party,rb` but is actually named `httparty.rb`. – Jörg W Mittag Sep 12 '20 at 20:29
  • 1
    The easiest way to use a case-sensitive filesystem is to use an operating system where case-sensitive filesystems are the norm. Which is basically every operating system except Windows or macOS and more or less every filesystem except FAT, exFAT, NTFS, ISO9660, HFS, and APFS. For example, ext4, btrfs, reiserfs, XFS, and JFS in Linux, FFS in FreeBSD, HAMMER2 in DragonflyBSD, etc., they all are case-sensitive. – Jörg W Mittag Sep 12 '20 at 20:35