-1

I'd like to write a ruby script that then calls another ruby script. Example, I'd like to run the "test1.rb" from my script. The test1.rb has been simplified to just do this:

print "1"

Then get the result (-> 1).

I tried to complete this problem with backticks or another executing command (%x[#{"test1.rb"}], system("test1.rb") etc.), but it didn't work.

So any idea how I call one script that then calls another script (either relinquishing total control or forking), and get the results?

Thanks

2
  • what did system("test1.rb") do? Did you try this with full (absolute) path to test.rb? Commented May 29, 2015 at 13:24
  • What do you mean by "it didn't work"? I'm guessing your attempt failed for the same reason typing test1.rb on the command line won't work: test1.rb isn't an executable file. You either need to execute the script with Ruby (e.g. system("ruby", "test1.rb")) or make the file executable with chmod and adding a shebang line. Commented May 29, 2015 at 13:31

4 Answers 4

4

You can simply require the file, which would load the code and execute it:

require_relative "path/test1"

For the sake of having controll over the the run code, I would advice to place your script in a method:

# In test1.rb
def exec_my_script
 puts 1
end

# In your main script
require_relative "path/test1"
exec_my_script

EDIT: Ok, since this does not seem to work for your usecase, you can read the file as string and eval the string as so:

result = eval(File.read("path/test1.rb"))
# do something with result

I do NOT like this approach, because it feels kinda "hacky" and is by all means insecure and it will only work if the last thing called in your test1 script returns the result you need...

Sign up to request clarification or add additional context in comments.

3 Comments

I really can't imagine that you would want to do this any other way. +1
Yes, it is a good idea, but I can't modify the test files.
I must call more same test files in the script and get the results after running.
1

You may want to use open3

require 'open3'
cmd = 'ruby test1.rb'
#You may change the contents of cmd like you would run it from the command line; like ruby [directory]/filename
Open3.popen3(cmd) do |stdin, stdout|
  var = stdout.read
  puts var
end

11 Comments

This might actually be what @huri.ferenc needs.
I think it's a very nice script.
I tried this, but returns an error message: "Can't find the file (1)"
Sorry, the error message correctly:
this will work if the ruby script is on the same directory with the one calling it. I'll edit the code
|
0
system('ruby test1.rb') # should do the trick

You can also make test1 executable (with chmod) and add the shebang line on top of test1.

You then can call

system("./test1.rb")

Comments

0

Ok, the system('ruby test1.rb') and system("ruby", "test1.rb") command worked.

And now, I like to set the returning value ("1") to a variable.

How I do it? It's possible to do that with backticks?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.