Jump to content

Passing Value To Ruby File Using Php System


bugcoder

Recommended Posts

Hello,

 

I am calling a simple ruby file using following php system command and its working fine so far.

Now I have an array that I want to pass to this ruby file. What is the best approach to do this?

 

 $rbFile = 'path/to/file.rb';
 system($cmd = "ruby $rbFile", $status);
 echo "\$cmd is $cmd\n\n";
 echo "\$status is {$status}\n\n";

Edited by bugcoder
Link to comment
Share on other sites

Depends upon how the Ruby script expects the arguments to be given.

 

ruby script can get that array in any variable and later that variable will be worked on for whatever purpose i would like to.

 

=========

"You'd do it the same way you would with a regular command line."

 

Any example of link will be very helpful for me

Edited by bugcoder
Link to comment
Share on other sites

If you're talking about an interpolable data format, I'd recommend using JSON. It's really light-weight and both languages have native support for it. If you're talking about how to actually pass that data into the program though, you can read from stdin with Ruby using the ARGF class.

Link to comment
Share on other sites

Thanks for all replies.

 

This is what I have got the solution with the help of my senior.

 

php file code:

$rbFile = "path/to/file.rb"; //ruby file full path
$reply = system($cmd = "ruby $rbFile ". escapeshellarg(json_encode(array('options' => 1, 'abd'=>'def'))), $status);
$ruby_response = json_decode($reply);
print_r($ruby_response);
//0 response is success
  if ($status === 0) {
    echo "yay!\n";
  } else {
    echo "boo!\n";
  }
  exit;

 

Ruby File code:

 

require 'json'
#using php passed arguments in ruby file
ARGV.each do |a|
 puts "Arguments: #{a}"
end
#replying to php file
puts [result:'ok', rows:400, out_file:'/tmp/abc.txt'].to_json
exit 0 # if success return 0
#exit  1 # if fail return 1

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.