rizidoro
rizidoro

Reputation: 13418

Ruby require ::File syntax

Why in some cases, requiring a file using File class in ruby are prepended with a blank namespace like the following code of config.ru

require ::File.expand_path('../config/environment',  __FILE__)

and in other places there isn't:

require File.expand_path('../../config/boot',  __FILE__)

Thanks

Upvotes: 3

Views: 459

Answers (1)

ilan berci
ilan berci

Reputation: 3881

The scope resolution operator that is explicitly specified (as in step one) ensures that the File class will be loaded up from the global namespace.

If a module has redefined the File class in it's namespace, the second version will pick that one over the global one.

In most cases, this can safely be disregarded and you can assume that your classes are being loaded by the global namespace even when the scope resolution operator is not defined

Upvotes: 4

Related Questions