Cover by Huw Collingbourne

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Including Modules from Files

So far, I’ve mixed in modules that have been defined within a single source file. Often it is more useful to define modules in separate files and mix them in as needed. The first thing you have to do in order to use code from another file is to load that file using the require method, like this:

require_module.rb

require( "./testmod.rb" )

Optionally, you may omit the file extension:

require( "./testmod" )  # this works too

If no path is given, the required file must be in the current directory, on the search path, or in a folder listed in the predefined array variable $:. You can add a directory to this array variable using the usual array-append method, <<, in this way:

$: << "C:/mydir"

Note

The global variable, $: (a dollar ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required