views:

106

answers:

2

Hi All,

i've js file that holds Array objects and data assigns

var A_1_val = new Array(7);
var B_1_txt = new Array(7);         

A_1_val[0] = '111';
B_1_txt[0] = 'utf8_content';

A_1_val[1] = '222';
B_1_txt[1] = 'bar';

etc..

need to get these arrays in ruby.

found http://github.com/jbarnette/johnson, but it can't correctly return an array object

another way is to eval js in ruby, similar to

  1. get name of arrays

  2. cut arrays initialize from js

  3. ruby eval

    A_1_val[0] = '111'

    B_1_txt[0] = 'utf8_content'

both ways are sucks. maybe you can suggests any ideas

thanks

+1  A: 

The easiest way to pass arrays (and a lot of other complex data structures) cross-language is with JSON. Use this to encode the array using JavaScript: http://www.json.org/js.html.

This will encode the array in a format that any langauge that supports JSON can use.

Use this: http://flori.github.com/json/ or this: http://github.com/brianmario/yajl-ruby to decode it with Ruby:

SimpleCoder
thanks, but this is not what i need. i'm trying to parse js array from 3rd service, so can't change the origin data format.my current version is:https://gist.github.com/8ee50a703182cf27c0aa
Oleg Keene
+1  A: 

You can use a JSON string to marshal the data between javascript and ruby:

#!/usr/bin/env ruby

require 'johnson'
require 'open-uri'
require 'yajl'

# Grab the source to the Javascript JSON implementation
json_js = open('http://www.json.org/json2.js').read
# Strip that silly alert at the top of the file
json_js.gsub!(/^(alert.*)$/, '/* \1 */')

# This is some Javascript you wanted to get something from
some_js = <<-EOF
var A_1_val = new Array(7);
var B_1_txt = new Array(7);         

A_1_val[0] = '111';
B_1_txt[0] = 'Ähtäri';

A_1_val[1] = 'Barsebäck slott';
B_1_txt[1] = '新宿区';
EOF

result = Johnson.evaluate(<<-EOF)
/* Include the JSON source code */
#{json_js}

/* Include the source code you wanted to get something from */
#{some_js}

/* Turn the things you wanted out into a string */
JSON.stringify([ A_1_val, B_1_txt ])
EOF

# Get the result back in ruby
ruby_result = Yajl::Parser.parse(result)

# Do something with it
puts ruby_result.inspect

which gives the output:

[["111", "Barseb\303\244ck slott", nil, nil, nil, nil, nil], ["\303\204ht\303\244ri", "\346\226\260\345\256\277\345\214\272", nil, nil, nil, nil, nil]]
eric
johnson-1.2.0/lib/johnson/spidermonkey/runtime.rb:36: [BUG] Segmentation fault
Oleg Keene
Sounds like you have something funny going on with your johnson install. The above code ran perfectly on my system.
eric
Your sample executed successfully, but when i tried the real data in swedish, got '[BUG] Segmentation fault'
Oleg Keene
I just updated the code to include UTF-8 characters. Does running the updated code cause a segfault for you?
eric
Nope. it's ok. so what's wrong with http://www.mol.fi/platser/scripts/scripts2.jsp?lang=sv
Oleg Keene
I've asked on the johnson issue tracker: http://github.com/jbarnette/johnson/issues/issue/32
eric
thanks, Eric...
Oleg Keene