bugcoder Posted October 20, 2012 Share Posted October 20, 2012 (edited) 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 October 20, 2012 by bugcoder Quote Link to comment https://forums.phpfreaks.com/topic/269711-passing-value-to-ruby-file-using-php-system/ Share on other sites More sharing options...
Christian F. Posted October 20, 2012 Share Posted October 20, 2012 Depends upon how the Ruby script expects the arguments to be given. Quote Link to comment https://forums.phpfreaks.com/topic/269711-passing-value-to-ruby-file-using-php-system/#findComment-1386554 Share on other sites More sharing options...
scootstah Posted October 20, 2012 Share Posted October 20, 2012 You'd do it the same way you would with a regular command line. Quote Link to comment https://forums.phpfreaks.com/topic/269711-passing-value-to-ruby-file-using-php-system/#findComment-1386571 Share on other sites More sharing options...
bugcoder Posted October 21, 2012 Author Share Posted October 21, 2012 (edited) 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 October 21, 2012 by bugcoder Quote Link to comment https://forums.phpfreaks.com/topic/269711-passing-value-to-ruby-file-using-php-system/#findComment-1386713 Share on other sites More sharing options...
Adam Posted October 21, 2012 Share Posted October 21, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/269711-passing-value-to-ruby-file-using-php-system/#findComment-1386717 Share on other sites More sharing options...
bugcoder Posted October 26, 2012 Author Share Posted October 26, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/269711-passing-value-to-ruby-file-using-php-system/#findComment-1387933 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.