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

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

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
method `logfile' for #<OptionParser:0x2cace70> (NoMethodError)

···

--
Posted via http://www.ruby-forum.com/.

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

The lesson: identical variable names that appear in different scopes are
not the same variable.

···

--
Posted via http://www.ruby-forum.com/.

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)
<CODE>

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
method `logfile' 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.

···

--
Posted via http://www.ruby-forum.com/\.

Brian Candler wrote in post #1016507:

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

The nitwits there are the cause of the problem.

···

--
Posted via http://www.ruby-forum.com/\.

7stud -- wrote in post #1016570:

Brian Candler 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/
$

···

--
Posted via http://www.ruby-forum.com/\.

Brian Candler wrote in post #1016602:

7stud -- wrote in post #1016570:

Brian Candler 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 why they are having problems 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.

···

--
Posted via http://www.ruby-forum.com/\.

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 Candler wrote in post #1016602:

7stud -- wrote in post #1016570:

Brian Candler 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.

--
Posted via http://www.ruby-forum.com/\.