Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7625361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:10:55+00:00 2026-05-31T05:10:55+00:00

I am trying to pass an array to a ruby script from a command

  • 0

I am trying to pass an array to a ruby script from a command line and facing some issue.

Here is the 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 is the output:

Array
[1, 2]
1
Array
[5, 6]
5

All is fine so far. Now I change my script to accept argument from the command line:

require 'pp'

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

Here is the output:

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

As you can see, the array gets passed as a string and arr[0] basically prints the ascii value.

So the question is how do I pass an array from the command line , hopefully in one line.
Also I believe this question is related to all shell invocations than just ruby ?

I am using bash shell.

Update:
Just updated the question to indicate that there can be multiple arrays at different positions

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:10:56+00:00Added an answer on May 31, 2026 at 5:10 am

    You can use eval although you might open a 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 it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to pass a ruby array from one C function to another
I am trying to pass an array of ints from C# to C++/CLI. Here's
I am trying to pass an array of interfaces from C# to C++/CLI. Here
I'm having a problem when trying to pass an array back to a COM
I'm trying to pass a byte array from inside my rails app into another
Im trying to pass an array from javascript to java servlet using Jackson, how
I'm trying to pass an array of integers from c# to c++ via a
I am trying to pass a array of date values from Objective C to
I am trying to pass Array from one class to another I used detailViewController.nameArray
im trying to pass an array of data to a PHP script using Jquery's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.