Incude Ruby files instead of all of your code in a one file. Single file hold all of your code just won’t do, except for small programs. Split file or files.

To prevent chaos, you can include ruby files and organize. If your program is object orientated, you can create a file for every class.

Download Free Ruby Ebook

Example

Load

The load method includes a Ruby file, each time a method is executed:

1
load 'filename.rb'

To be clear: the load method add a ruby file into your code, every time a script is executed. It does not work like a module or library.

Require

The require methods loads the given file only once.

1
require 'filename'  

If you want to load a module, use require().
To execute code, use load().

Require doesn’t use the rb extension (filename.rb), because not all extensions end with .rb.

Example

Create two files in the same directory, one named example.py

1
2
3
require './hello'

hello()

and another file named hello.rb

1
2
3
def hello
puts "Hello"
end

When executing, it will load hello.rb and call its method.