Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pass the string "*.*" to ruby as a command line parameter?

Tags:

ruby

argv

code:

  #test_argv.rb
  puts "length: #{ARGV.length} "
  ARGV.each do |a|
    puts "Argument: #{a}"
  end

If I supply the string "*.*" (with or without quotes) when I call the above, I get the following output:

  C:\test>test_argv *.*
  length: 5
  Argument: afile.TXT
  Argument: bfile.TXT
  Argument: cfile.TXT
  Argument: dfile.TXT
  Argument: somethingelse.TXT

i.e., a listing of the files in c:\test.

Other values, like "s*.*" returns somethingelse.TXT, as you'd expect if you were doing file operations -- but I'm not.

But this behaves as would have expected:

  C:\test>test_argv asdf
  length: 1
  Argument: asdf

So my question is, how can I make a user-friendly script that will take "*.*" (etc) as a command line parameter? In addition, where is this documented/explained?

Edit: this happens on windows and linux, 1.8.7 and 1.9.2

like image 788
davej Avatar asked Jun 07 '11 16:06

davej


People also ask

How do you pass command line arguments in Ruby?

How to Use Command-Line Arguments. In your Ruby programs, you can access any command-line arguments passed by the shell with the ARGV special variable. ARGV is an Array variable which holds, as strings, each argument passed by the shell.

What is * args Ruby?

In the code you posted, *args simply indicates that the method accepts a variable number of arguments in an array called args . It could have been called anything you want (following the Ruby naming rules, of course).

How do I run a command line in Ruby?

Press Ctrl twice to invoke the Run Anything popup. Type the ruby script. rb command and press Enter . If necessary, you can specify the required command-line options and script arguments.


1 Answers

You may need to put this into literal quotes:

test_argv "*.*"

The quotes should avoid having the command-line arguments get expanded on you prematurely.

like image 114
tadman Avatar answered Oct 08 '22 00:10

tadman