-2

Hooray, its dumb question day! ;)

I know enough ruby to be dangerous but dont know a lot of basic foundational things. Can anyone direct me to some documentation or help me out with how to get my ruby code to take the first argument i throw at it (a file name) and store the filename in a variable? I've tried ARGV[0] and ARGV.first.

require 'mysql'
require 'nessus'

begin


filename = ARGV.first
scanTime = Time.now.to_i

Nessus::Parse.new(filename, :version => 2) do |scan|
....
Meier
  • 3,858
  • 1
  • 17
  • 46
dobbs
  • 1,089
  • 6
  • 22
  • 45

2 Answers2

1
#myprog.rb
p ARGV

fname = ARGV[0]
puts fname

puts File.read(fname)

--output:--
$ cat data.txt
John: a,123,b,456
Sally: c,789,b,0

~/ruby_programs$ ruby myprog.rb data.txt 10 hello
["data.txt", "10", "hello"]
data.txt
John: a,123,b,456
Sally: c,789,b,0
7stud
  • 46,922
  • 14
  • 101
  • 127
0

First of all, you are on the right track. ARGV[0] or ARGV.first is the right way to grab the first argument. I suspect that you are not sending/receiving the arguments properly. In that case, doing p ARGV will help you figure that out.

To see all the arguments you are receiving, do:

puts ARGV.inspect

or,

p ARGV

This will give you an array of arguments you pass. Like this:

➜ ruby test.rb foo bar baz
# => ["foo", "bar", "baz"]

Then, according to your need, do:

puts ARGV.first
puts ARGV.last

and save the file_name like this:

filename = ARGV.first

or,

filename = ARGV[0]

Here is doc for ARGV.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110