What I understand from require statement is that the argument to it is a
file, where as include requires module/class name.
But once I do a require for a specific ruby file which has various
modules
and classes, doesn’t these classes/modules get included automatically?
infact they do get automatically as I have tried doing so.
But in various files of ruby library I found that people use include
“modulename” after a require statement for the file which contains that
module.
Any reasons?
Thanks,
Jatinder
Jatinder S. wrote:
Any reasons?
Thanks,
Jatinder
Require is purely used to ‘embed’ files in other files.
Include is used on modules, and adds so-called “MixIn”-functionality.
It means you “import” the methods of the given Module into the current
class (or Module?).
So, basically, you have a Module that provides some semi-generic
functionality, then you have a class that wants to take advantage of
this functionality.
Example:
module A
def b
“bar”
end
end
class B
include A
end
foo = B.new
foo.b
=> “bar”
I hope this helps!
Ps. this explanation might be horrible, and it might be wrong; I’m not a
“professional” rubyist.