<div dir="ltr"><div>Hello,</div><div><br></div><div>I&#39;ve being struggling with this issue for a couple of days, trying to get to a reproducible error.  I must be blind to something really obvious, but I can&#39;t find it.  I tried to reduce the code as much as possible and here it is:</div><div><br></div><div> module R<br><br>  #----------------------------------------------------------------------------------------<br>  #<br>  #----------------------------------------------------------------------------------------<br><br>  def self.eval(string)<br>    Polyglot.eval(&quot;R&quot;, string)<br>  end<br>    <br>  #----------------------------------------------------------------------------------------<br>  #<br>  #----------------------------------------------------------------------------------------<br><br>  def self.parse_arg(arg)<br>    # if this is an R object, leave it alone<br>    if (Truffle::Interop.foreign?(arg) == true)<br>      return arg<br>      # convert integer to double, this is how R understands a number.<br>    elsif (arg.is_a? Integer)<br>      arg.to_f<br>    end<br>  end<br>  <br>  #----------------------------------------------------------------------------------------<br>  # Parse all arguments into an R list of arguments<br>  #----------------------------------------------------------------------------------------<br><br>  def self.parse2list(*args)<br>    params = Polyglot.eval(&quot;R&quot;, &quot;list()&quot;)<br>    args.each_with_index do |arg, i|<br>      params = R.eval(&quot;`[[&lt;-`&quot;).call(params, i+1, parse_arg(arg))<br>    end<br>    params<br>  end<br><br>  #----------------------------------------------------------------------------------------<br>  # @param function [R function (Interop)] R function to execute<br>  # @args [Argument list] arguments for the function<br>  #----------------------------------------------------------------------------------------<br><br>  def self.exec_function(function, *args)<br>    pl = R.parse2list(*args)</div><div>    # calls the function in R with the parameter list<br></div><div>    R.eval(&quot;do.call&quot;).call(function, pl)<br>  end<br><br>end<br><br>#========<br># First example: the return value is an array<br>#=======<br>func = Polyglot.eval(&quot;R&quot;, &quot;c&quot;)<br>vect = R.exec_function(func, 1, 2, 3, 4, 5)<br><br>func = Polyglot.eval(&quot;R&quot;, &quot;`[`&quot;)<br>v1 = R.exec_function(func, vect, -2)<br><br>v2 = R.exec_function(func, v1, 2)<br>p v2.to_s<br>#=========<br></div><div>The printed value here is:</div><div>&gt;&gt; [3]<br></div><div><br>#========<br># Second example: the return value is a scalar.  The only difference<br># between the first example and this one is that function ex_f<br># is defined on the global scope.  This function is an exact copy<br># of function exec_function defined in the R module<br>#========<br>def ex_f(function, *args)<br>  pl = R.parse2list(*args)<br>  R.eval(&quot;do.call&quot;).call(function, pl)<br>end<br><br>func = Polyglot.eval(&quot;R&quot;, &quot;c&quot;)<br>vect = R.exec_function(func, 1, 2, 3, 4, 5)<br><br>func = Polyglot.eval(&quot;R&quot;, &quot;`[`&quot;)<br>v1 = R.exec_function(func, vect, -2)<br><br>v2 = ex_f(func, v1, 2)<br>p v2.to_s<br>#========<br></div><div>The printed result here is:</div><div>&gt;&gt; 3.0<br></div><div><br></div><div><br></div><div>Note that the only difference between example 1 and 2 is the definition of function ex_f, copy of execute_function, in the global scope.  I can&#39;t understand the printed values and the change from Array to double.</div><div><br></div><div>I have executed this as:</div><div><br></div><div>ruby --polyglot --jvm -Xsingle_threaded strange.rb<br></div><div><br>-- <br><div dir="ltr" class="gmail_signature">Rodrigo Botafogo<br><br></div></div></div>