ruby - passing array as a command argument -


i trying pass array ruby script command line , facing issue.

here problem:

require 'pp'  def foo(arr1, var, arr2, var2)   puts arr1.class   pp arr1   pp arr1[0]   puts arr2.class   pp arr2   pp arr2[0] end  foo [1, 2], 3, [5, 6], 8 

here output:

array [1, 2] 1 array [5, 6] 5 

all fine far. change script accept argument command line:

require 'pp'  def foo(arr1,var)   puts arr1.class   pp arr1   pp arr1[0] end foo argv[0],3 

here output:

jruby test.rb [1, 2], 3, [5, 6], 8 string "[1," 91 string "2]," 50 

as can see, array gets passed string , arr[0] prints ascii value.

so question how pass array command line , in 1 line. believe question related shell invocations ruby ?

i using bash shell.

update: updated question indicate there can multiple arrays @ different positions

you can use eval although might open security hole:

require 'pp'  def foo(arr1, var, arr2, var2)   puts arr1.class   pp arr1   pp arr1[0]   puts arr2.class   pp arr2   pp arr2[0] end  eval("foo " + argv.join(" ")) 

result

array [1, 2] 1 array [5, 6] 5 

hope helps


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -