I have the following hypothetical script that requires arguments to run.
The command line execution looks like this:
'ruby distance_traveled.rb 60 2.5'
The output would be the following:
'You have traveled a distance of 150 miles.'
How would I run the above execution from inside another script, and
capture the output above (the 150 miles portion at least)?
Thanks in advance!
···
--
Posted via http://www.ruby-forum.com/.
I've faced an exactly same problem when was testing command line utilities, by capturing an output and comparing it with expected one. Here is a gem for it:
http://github.com/fkocherga/cmd_line_test
You may look on running and capturing code there.
···
On Dec 29, 2009, at 7:11 PM, John Smith wrote:
I have the following hypothetical script that requires arguments to run.
The command line execution looks like this:
'ruby distance_traveled.rb 60 2.5'
The output would be the following:
'You have traveled a distance of 150 miles.'
How would I run the above execution from inside another script, and
capture the output above (the 150 miles portion at least)?
Thanks in advance!
--
Posted via http://www.ruby-forum.com/\.
--
Fedor Kocherga
http://sidenotes.kocherga.info/
output = `ruby distance_traveled.rb 60 2.5`
The backticks cause the standard output of the command to be captured and stored in a Ruby string.
Gary Wright
···
On Dec 29, 2009, at 8:11 PM, John Smith wrote:
I have the following hypothetical script that requires arguments to run.
The command line execution looks like this:
'ruby distance_traveled.rb 60 2.5'
The output would be the following:
'You have traveled a distance of 150 miles.'
How would I run the above execution from inside another script, and
capture the output above (the 150 miles portion at least)?
output = `ruby ~project/lib/distance_traveled.rb 60 2.5` 
Or perform string interpolation, if you don't want to hardcode anything:
path = "~/project/lib/"
script = "script.rb"
args = "my args"
output = `#{path}#{script} #{args}`
<warning type="security implications in accepting user input" />
···
On 30.12.2009 14:06, John Smith wrote:
One more quick question. If distance_traveled.rb sits in a different
directory than the current script that's calling it, how would I specify
it? For example, say it is in ~project/lib/distance_traveled.rb
--
Phillip Gawlowski
The command is parsed by the shell so ~project/lib/distance_traveled.rb should work or a relative or absolute path of course.
Gary Wright
···
On Dec 30, 2009, at 8:06 AM, John Smith wrote:
One more quick question. If distance_traveled.rb sits in a different
directory than the current script that's calling it, how would I specify
it? For example, say it is in ~project/lib/distance_traveled.rb
Gary Wright wrote:
One more quick question. If distance_traveled.rb sits in a different
directory than the current script that's calling it, how would I specify
it? For example, say it is in ~project/lib/distance_traveled.rb
The command is parsed by the shell so ~project/lib/distance_traveled.rb
should work or a relative or absolute path of course.
Gary Wright
Thanks for the help!
However, I should also add that in the above example,
distance_traveled.rb requires other .rb files sitting in the same lib
directory.
I have no problem running distance_traveled.rb from inside lib. However,
from another directory, when I try to run the program using relative
path (ie. 'ruby ../../lib/distance_traveled.rb'), I get
"distance_traveled.rb:1:in `require': no such file to load -- ", in
reference to the other .rb file(s) in the lib directory.
Any suggestions for this issue? Thanks again!
···
On Dec 30, 2009, at 8:06 AM, John Smith wrote:
--
Posted via http://www.ruby-forum.com/\.
If the files aren't in standard library locations, you'll want to make sure that you name them relative to the source file using something like:
require File.join(File.expand_path(File.dirname(__FILE__)), 'otherfile')
This ensures that the require method sees a fully qualified path to 'otherfile' where 'otherfile' is found relative to the directory containing the source file instead of relative to the current working directory of the process.
Gary Wright
···
On Dec 30, 2009, at 12:24 PM, John Smith wrote:
However, I should also add that in the above example,
distance_traveled.rb requires other .rb files sitting in the same lib
directory.
I have no problem running distance_traveled.rb from inside lib. However,
from another directory, when I try to run the program using relative
path (ie. 'ruby ../../lib/distance_traveled.rb'), I get
"distance_traveled.rb:1:in `require': no such file to load -- ", in
reference to the other .rb file(s) in the lib directory.
Any suggestions for this issue? Thanks again!