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";

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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.