How to execute ruby file from other ruby file?

D

Dirk Meijer

------=_Part_3087_10746843.1136909690107
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

require 'filename'

you're welcome


2006/1/10 said:
How to execute ruby file from other ruby file?

Thanks!

------=_Part_3087_10746843.1136909690107--
 
J

James Britt

Dirk said:
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
 
D

David Vallner

Szczepan said:
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 Vallner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top