Forum: Ruby How to execute ruby file from other ruby file?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
16319aba44da117ca73484d1ce32227b?d=identicon&s=25 Szczepan Faber (szczepiq)
on 2006-01-10 17:11
How to execute ruby file from other ruby file?

A16652fd5d83c0473bd1e39d9a2117a6?d=identicon&s=25 Dirk Meijer (Guest)
on 2006-01-10 17:17
(Received via mailing list)
require 'filename'

you're welcome

2006/1/10, Szczepan Faber <>:
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-01-10 17:29
(Received via mailing list)
Dirk Meijer wrote:
> require 'filename'
> you're welcome


`ruby filename.rb`  # back-tics

which will trigger code that is testing to see if the file is called
directly. E.g.:

if __FILE__ == $0
   # do me!

--       - Ruby Help & Documentation - The Journal By & For Rubyists      - The Ruby Store for Ruby Stuff     - Playing with Better Toys   - Building Better Tools
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-01-10 17:32
(Received via mailing list)
On Wed, 11 Jan 2006, Szczepan Faber wrote:

> How to execute ruby file from other ruby file?
> Thanks!

load 'a.rb'

430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-11 05:36
(Received via mailing list)
Szczepan Faber wrote:

>How to execute ruby file from other ruby file?
To provide a comparison of the previously mentioned approaches:

`ruby foo.rb` creates a completely separate interpreter, which you might
or might not want. You can't directly access anything defined in one
script in the other.

require is more commonly used to load libraries, since it will only
process a file once; On the other side, load will always process the

For example, if you have (in the same directory) the files:

1. foo.rb

    puts "FOO"

2. bar.rb

    puts "BAR"

3. test.rb

    require 'foo'
    require 'foo'
    load 'bar.rb'
    load 'bar.rb'

Then, unless I'm very much mistaken, the output will be:


David Vallner
This topic is locked and can not be replied to.