Javascript object instances in Ruby?

If I have a javascript object, I usually interact with the object and its methods as follows:

var obj = someObject.getInstance(); var result = obj.someMethod(); 

where someMethod is defined as follows:

 someObject.prototype.someOtherMethod = function() { //do stuff }; someObject.prototype.someMethod = function(foo) { this.someOtherMethod(); }; 

However, I get an error when I want to call someMethod in Ruby via ExecJS:

 context = ExecJS.compile(# the javascript file) context.call('someObject.getInstance().someMethod') # Gives a TypeError where Object has no method 'someOtherMethod' 

On the other hand, the functions defined in the javascript module work fine:

 someFunction = function() { // do stuff }; # in Ruby context.call('someFunction') # does stuff 

Can ExecJS handle Javascript objects and their methods, or can I only call functions with it?

As for the specific application, I look at https://github.com/joenoon/libphonenumber-execjs , but the parse function in Libphonenumber does not work for the above reason.

+4
source share
1 answer

An answer was found through some experiments. I managed to get the desired functionality using context.exec () instead of calling.

 js = <<JS var jsObj = someObject.getInstance(); var res = jsObj.someMethod(); return res; JS context.exec(js); 

However, if your method returns a Javascript object, you must first serialize it or otherwise analyze the results so that ExecJS can be returned to a suitable Ruby object.

+3
source

All Articles