简体   繁体   中英

Command line interaction using Ruby

I have a Rails application and am trying to communicate with a Java program via the CLI.

When I run the Java code using system :

system "java askQuestion"

it prompts for user input and waits for an answer, such as "What is your age?"

I want to pass a value in from a variable, and capture the output.

How can I interact with the CLI and run the command?

I did research but I couldn't find anything or I missed the correct term to search.

Solution: IO.popen

update --> I found here what exactly I want and share maybe help someone else too , http://ruby.bastardsbook.com/chapters/external-programs/

Kernel#system just executes the command in a subshell, returning the result ( true / false ) of process start.

To catch the output, use backticks (or %x|| ) .

To interact with a shell, one might use IO#popen , but in your case I would just stick to executing

output = `echo 37 | java askQuestion`

The above will pass the output of echo (37 in this particular case) to the Java process. The response of the Java process will be stored in the output variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM