Does requiring a gem load everything, including things I don't use?

Go To StackoverFlow.com

2

Assume x is a gem, that contains both Hello and Goodbye classes.

If I write a program that require 'x', but only uses the Hello class. Is the Goodbye class loaded as well?

2012-04-03 20:45
by Mr. Demetrius Michael


4

You include scripts or files, not gems.

With

require 'x' 

you load the file x.rb. Which x.rb you load is defined by the search path, the search pathes can be modified by gem definitions (what you didn't use in your example code).

Everything inside the file x.rb is loaded. If x.rb contains other require commands, those files are also loaded.

2012-04-03 20:53
by knut
Check out the autoload if you wish to overcome this problem - Aleksander Pohl 2012-04-03 21:09
@AleksanderPohl That's not necessarily true, if file x has Hello & Goodbye, using one of them will still load both since they're still in the same file - Andrew Marshall 2012-04-03 21:42
I haven't found that both classes are in the same file in the description of the problem. They are only defined in the same gem - Aleksander Pohl 2012-04-03 21:55
Thanks, I meant the question to be open ended for both cases. Sorry if it wasn't clear. I posted a followup Q: http://stackoverflow.com/questions/10002405/load-only-what-classes-are-being-used-in-rub - Mr. Demetrius Michael 2012-04-03 22:35