Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between `require` and `gem`

Tags:

What's the difference between require 'cool_lib' and gem 'cool_lib'?

like image 595
TK. Avatar asked Jan 30 '10 08:01

TK.


People also ask

What are gem files used for?

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.

What is gem require false?

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.

What does require do in Rails?

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.

What is require in Ruby?

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.


1 Answers

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.

like image 119
Peter Avatar answered Oct 12 '22 08:10

Peter