Stuck at an Issue with OptParse and File IO

Hi,
i am picking default values that i am using in optparser class,
something like this…

options.logfile = LOGFILE
options.results_dir = Pathname.new(ENV[‘TEMP’])

PROG = Pathname.new($0).basename
EFIX = PROG.sub(/…*$/,’’)
LOGFILE = Pathname.new("#{ENV[‘TEMP’]}/#{FPREFIX}.log")

Below is part of the code that is throwing following exception

fh = options.logfile.open('w') --> This line is failing log = TeeLogger.new(fh,$stderr) log.level = Logger::INFO log = TeeLogger.new(fh,$stderr)

Im running it on eclipse and on also on cmd i get the same error…So
when i run=>
ruby opttest.rb

"C:/Users/user1/workspace/Test/opttest.rb:153:in <main>': undefined methodlogfile’ for #OptionParser:0x2cace70 (NoMethodError)

require ‘optparse’
require ‘ostruct’

arr = [1, 2, 3]
arr.logfile = “my_file.txt”

–output:–
undefined method `logfile=’ for [1, 2, 3]:Array (NoMethodError)

options = OpenStruct.new
options.logfile = “my_file.txt”
puts options.logfile

–output:–
my_file.txt

def do_stuff
options = OpenStruct.new
options.logfile = “my_file.txt”
puts options.logfile
end

do_stuff()

–output:–
my_file.txt

options = OptionParser.new
puts options.logfile

–output:–
undefined method `logfile’ for Usage: ruby [options] (NoMethodError)

The lesson: identical variable names that appear in different scopes are
not the same variable. And people who write code like that are nitwits.

ideal one wrote in post #1016461:

Hi,
i am picking default values that i am using in optparser class,
something like this…

options.logfile = LOGFILE
options.results_dir = Pathname.new(ENV[‘TEMP’])

PROG = Pathname.new($0).basename
EFIX = PROG.sub(/…*$/,’’)
LOGFILE = Pathname.new("#{ENV[‘TEMP’]}/#{FPREFIX}.log")

That code wouldn’t work, because you’re using LOGFILE (in the first
line) before you’ve assigned to it (the last line); and also you’ve not
shown any initial creation of the ‘options’ object anyway.

So the best thing you can do is to write a single, complete, standalone
program which demonstrates your problem - then it’s easy for us to run
it and explain what’s wrong. Although usually, I think you’ll find that
in the process of boiling it down to this test case, you’ll work out
what the problem was anyway.

fh = options.logfile.open(‘w’) --> This line is failing
log = TeeLogger.new(fh,$stderr)
log.level = Logger::INFO
log = TeeLogger.new(fh,$stderr)

Im running it on eclipse and on also on cmd i get the same error…So
when i run=>
ruby opttest.rb

"C:/Users/user1/workspace/Test/opttest.rb:153:in <main>': undefined methodlogfile’ for #OptionParser:0x2cace70 (NoMethodError)

That means: your ‘options’ object, which is an instance of class
OptionParser, doesn’t have a method called ‘logfile’.

So you need to go to the documentation for OptionParser and see what
method you should be calling instead.

Brian C. wrote in post #1016507:

So you need to go to the documentation for OptionParser and see what
method you should be calling instead.

There be nitwits there.

7stud – wrote in post #1016570:

Brian C. wrote in post #1016507:

So you need to go to the documentation for OptionParser and see what
method you should be calling instead.

There be nitwits there.

Ruby’s documentation is often poor, but there’s always the source code
you can look at.

In this case, a simple check shows that there is no ‘logfile’ method
anywhere in the OptionParser library.

$ grep -iR logfile /usr/lib/ruby/1.8/optparse.rb
/usr/lib/ruby/1.8/optparse/
$

Brian C. wrote in post #1016602:

7stud – wrote in post #1016570:

Brian C. wrote in post #1016507:

So you need to go to the documentation for OptionParser and see what
method you should be calling instead.

There be nitwits there.

Ruby’s documentation is often poor, but there’s always the source code
you can look at.

I thought I would show the op how the nitwits misled him instead.

In this case, a simple check shows that there is no ‘logfile’ method
anywhere in the OptionParser library.

The error message already demonstrated that.

Thanks U all for ur reply…actually that was part of code wht i
attached here and not the complete stuff…anyway i got it resolved
differently…

special Thanks for 7Stud, who i believe needs to start from
“Kindergarten” AGAIN… for his nitwit advice…!!!

Cheers

7stud – wrote in post #1016652:

Brian C. wrote in post #1016602:

7stud – wrote in post #1016570:

Brian C. wrote in post #1016507:

So you need to go to the documentation for OptionParser and see what
method you should be calling instead.

There be nitwits there.

Ruby’s documentation is often poor, but there’s always the source code
you can look at.

I thought I would show the op how the nitwits misled him instead.

In this case, a simple check shows that there is no ‘logfile’ method
anywhere in the OptionParser library.

The error message already demonstrated that.