What's the difference between require 'cool_lib'
and gem 'cool_lib'
?
A Gemfile is a file that is created to describe the gem dependencies required to run a Ruby program. A Gemfile should always be placed in the root of the project directory.
By using require: false , gems will not be loaded until the first time where they're explicitly required. This means it's up to you to decide when to require each gem. Normally, you'll do so close to where you actually make use of them.
require can also be used to load only a part of a gem, like an extension to it. Then it is of course required where the configuration is. You might be concerned if you work in a multi-threaded environment, as they are some problems with that. You must then ensure everything is loaded before having your threads running.
In Ruby, the require method is used to load another file and execute all its statements. This serves to import all class and method definitions in the file.
gem
gives you more options as to which exact gem you load: see here for plenty of details. In this example, there's no effective difference.
I'd recommend using require
unless you specifically need the power of gem
- it seems to be what most people use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With