开发者

how to create a lib file in rails that executes a script file?

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开发者_StackOverflow a lib file?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜