Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require file inside module?

Tags:

ruby

I saw the following source code in someone's repository:

module Twitter
  module Bootstrap
      module Rails
        require 'twitter/bootstrap/rails/engine' if defined?(Rails)
      end
   end
end

require 'less-rails'
require 'twitter/bootstrap/rails/bootstrap' if defined?(Rails)

Source

I want to know what's difference when we put require in a module?

like image 774
Weihang Jian Avatar asked Jul 25 '12 03:07

Weihang Jian


1 Answers

There is no difference as far as the require is concerned, i.e., require always loads the file into the global namespace.

It should be noted that, in this case, the inner require will always run, since Rails at that point refers to the module it is within, so the if statement there will always evaluate to true.

This means the code is equivalent to the possibly less confusing:

module Twitter
  module Bootstrap
    module Rails
    end
  end
end

require 'twitter/bootstrap/rails/engine'
require 'less-rails'
require 'twitter/bootstrap/rails/bootstrap' if defined?(Rails)
like image 85
Andrew Marshall Avatar answered Oct 16 '22 11:10

Andrew Marshall