how do i pass php array's to ruby script as an argument so that ruby script can read php's array. ?
Or any neutral serialized format for that matter (XML, CSV, etc.).
Matthew Vines
2009-08-31 22:47:21
okay, i serialized the array to JSON....and i pass it as argument to ruby script....but ruby complains because of the double quotes.... additionally, how do i parse JSON and deserialize it in ruby ?
2009-08-31 23:13:14
+2
A:
You could use JSON:
$ cat f1.php
<?php
$a = array(1, 2, 3);
$cmd = 'ruby f2.rb \'' . json_encode($a) . '\'';
printf("%s\n", `$cmd`);
?>
$ cat f2.rb
require 'rubygems'
require 'json'
s = JSON.parse ARGV[0]
puts s
puts s.class
$ php f1.php
1
2
3
Array
DigitalRoss
2009-08-31 23:18:49
Hmm, you mean you can't reproduce my example? Perhaps we should simplify things slightly and try:$ ruby f2.rb '[1,2,3]'
DigitalRoss
2009-08-31 23:37:22
that works.... but when i do exec("ruby f2.rb '[1,2,3]'")the php script runs, and there is no ruby "puts"
2009-08-31 23:39:48
The backtick operator is equivalent to shell_exec() rather than exec(), but note the optional second parameter to exec(). The actual return value from exec() is just the last line from the command output.
DigitalRoss
2009-09-03 15:41:44
A:
something like this this
require 'json'
data = ARGV[0]
result = JSON.parse(data)
ez
2009-08-31 23:22:40
A:
JSON is a better idea, but if you must use PHP's serialize()
function you can unserialize it in Ruby using this library: http://www.aagh.net/projects/ruby-php-serialize
dubek
2010-05-25 09:23:55