A_01
A_01

Reputation: 1141

Ruby : Pass array in command line argument

I want to pass array in argument in such a way suppose process.rb is my script and the argument will be like:

i/p

process.rb server{1..4} 
process.rb prodserver{2..3} 
process.rb devserver3

The process.rb should accept all the inputs and parse it in such a way that when I print the variable which holds the arguments give me below result.

o/p
    puts arguments

    server1
    server2
    server3
    server4
    or
    prodserver2
    prodserver3
    or
    devserver3

I have a shell script which does the same:

for i in "$@"
do
echo $i
done

i/p
server{1..4} 
o/p
server1server2server3server4

I wanted to have the same logic in the ruby. Since I am a new bie in ruby I am not able to find the same on google. Please let me know how I can get this output or any article about the related to my question

Upvotes: 1

Views: 2447

Answers (4)

Jörg W Mittag
Jörg W Mittag

Reputation: 369428

The list is expanded by the shell before it ever hits your script. In other words, both your shell script and your Ruby script do not receive a single argument server{1..4} but rather they receive four arguments server1 server2 server3 server4, before they even start interpreting the arguments themselves.

You can simply just iterate over those, there is no need to parse the {1..4} shell expansion syntax yourself because you will never see it! It is already parsed and expanded by the shell before the shell passes off the arguments to your script.

ruby -e 'p ARGV' -- server{1..4}
# ["server1", "server2", "server3", "server4"]

Upvotes: 4

Jim Stewart
Jim Stewart

Reputation: 17323

Command-line arguments in Ruby end up in ARGV. You can duplicate your shell script's functionality by iterating over that:

ARGV.each do |a|
    puts a
end

Upvotes: 3

peter
peter

Reputation: 42182

If I understand you correctly you want to expand the range that comes in string form from your argument ARGV[0] ? My samples use a string to demonstrate it workd, replace the string by ARGV[0]

def expand_range arg
  string, range = arg.split("{") #split arg in string part and rangestring part
  if range #if a range is given
    # parse the rangestring to an range by splitting the string on .. 
    # and splash this array to both its elements, convert them to integer
    # and transform into a real range
    # and enumerate each number in the range
    Range.new(*range.split("..").map(&:to_i)).each do |val|
      #concatenate the string part with the number
      p "#{string}#{val}"
    end
  else #else just pass the string
    p string
  end
end

expand_range 'server{1..4}'
# "server1"
# "server2"
# "server3"
# "server4"

expand_range 'devserver3' 
#"devserver3"

Personally I would return an array and print that instead of printing in the method itself, that would be more multifunctional.

Upvotes: 1

MBO
MBO

Reputation: 30985

#!ruby

ARGV.each do |i|
  puts i
end

Basically ARGV holds all arguments passed to program, and puts prints string with new line added (the same as echo without -n flag in shell).

Upvotes: 3

Related Questions