The Argument to require

The argument to require is a filename—sort of. Actually it's the name of an extension, and Ruby extensions are kept in files. So it's a filename. Sort of…

When you say require "mymodule", Ruby doesn't take mymodule as a straight filename. If it did, it wouldn't find it, since you have no file called mymodule—only one called mymodule.rb. But Ruby does find your file, because Ruby adds the extension .rb to the argument to require, if that extension isn't there already.

Ruby also respects path information that you include in the argument to require. If you provide an absolute path (such as /home/mydir/rubystuff/mymodule), Ruby treats it as an absolute path (still performing the trick of adding .rb on the filename at the end). ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.