0

I want to create a file my_file.rb inside #{Rails.root}/lib/my_file.rb that executes a script my_script residing in #{Rails.root}/script/my_script. I intend to use it like so:

script/first_script # this first script will call a method in
# lib/my_file.rb that will run the script/my_script file.

How can i create such a lib file that can execute a script? and what is the command to execute a script from a lib file?

3
  • Isn't it easier to use script/runner any_file.rb? Why do you need so complex structure? Commented Jun 16, 2011 at 14:25
  • the script does some tasks, and we usually execute it like so: script/any_file > result.log 2> error.log, and then we manually parse the result.log. we need to rewrite the script so that it runs a lib file, which calls the second script using: script/second_script result.log 2> error.log, and then the lib file can access and parse the result.log and present results. Commented Jun 16, 2011 at 14:34
  • 1
    You might need the popen3 approach if you want to do that, as it bypasses the need for intermediate log-files. I've updated my answer. Commented Jun 16, 2011 at 15:10

2 Answers 2

2

If by "execute the script" you mean run it in a separate process, then what you need is the system call.

Update:

You can also use the popen3 method if you want to capture STDOUT and STDERR independently. From the example:

Open3.popen3("#{Rails.root}/script/my_script") do |in, out, err|
  # ...
end
Sign up to request clarification or add additional context in comments.

Comments

1

here's the list of various options to run a process (in answer to your question, basically you define the method within that lib/xxx.rb, then call that method, and within that method, you run system or what not: http://en.wikibooks.org/wiki/Ruby_Programming/Running_Multiple_Processes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.