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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Szczepan F. (Guest)
on 2006-01-10 18:11
How to execute ruby file from other ruby file?

Thanks!
Dirk M. (Guest)
on 2006-01-10 18:17
(Received via mailing list)
require 'filename'

you're welcome


2006/1/10, Szczepan F. <removed_email_address@domain.invalid>:
James B. (Guest)
on 2006-01-10 18:29
(Received via mailing list)
Dirk M. wrote:
> require 'filename'
>
> you're welcome

Or,

`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!
end

James
--

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - The Journal By & For Rubyists
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
unknown (Guest)
on 2006-01-10 18:32
(Received via mailing list)
On Wed, 11 Jan 2006, Szczepan F. wrote:

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

load 'a.rb'

-a
David V. (Guest)
on 2006-01-11 06:36
(Received via mailing list)
Szczepan F. wrote:

>How to execute ruby file from other ruby file?
>
>Thanks!
>
>
>
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
file.

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:

    FOO
    BAR
    BAR


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