Home > ruby > Multiple ruby files work together

Multiple ruby files work together

October 29th, 2009 Leave a comment Go to comments

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



Categories: ruby Tags:
  1. No comments yet.
      
                  
      

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word