Multiple ruby files work together
When writing the first few Ruby programs, there is a tend to place all the code in a single file. But as time passes by and the Ruby programs grow, it is nature that at some point we have to break our code up into logical groupings and place each group in a separate file or files.
following is an examle:
in foo.rb:
puts “it is foo”
$foo = 3 # $ for global variable
in bar.rb:
puts “it is bar”
$bar = 3 # $ for global variable
in test.rb:
require ‘foo’ # pay attention –no “.rb”
load ‘bar.rb’ # pay attention — “.rb” is there
test = 1 + $foo + $bar
when execute test.rb, it shows :
it is foo
it is bar
9