From: Justin Collins Date: 2009-01-21T11:21:06+09:00 Subject: Re: cannot get attr_accessor to work Tom Cloyd wrote: > I've been staring at this for a couple of hours, and I cannot crack > the nut. This SHOULD work, but I'm obviously screwing up. Just cannot > see where. I'm carefully following several examples, but...I cannot > get access to my instance variables. > > Here's a stripped down version of the code - > > # SetNet.rb > > def main > # Set up logging > run_log = Manage_log.new( 'logfile.txt' ).open > log = run_log.log > logging_now = run_log.lgg # @logging_now = false > # @log, @logging_now = manage_log( 'open' ) > end > > # Creates program log file, setting default logging level of INFO. > Current logging is appended to existing log content. Closes log when > requested by user. > # > # * _op_ - Requested operation: _open_ or _close > # > class Manage_log attr_accessor :log, :lgg > def initialize( logFileName ) > @logFileName = logFileName > end > def open #create log file > log_main = File.open( @logFileName, File::WRONLY | File::APPEND ) > @log = Logger.new( log_main ) > @log.datetime_format = "%Y-%m-%d %H:%M:%S" > @lgg = true > > # set logging level > @log.level = Logger::INFO # I.e., only INFO level and above will be > logged > @log.info( '===== START logging' ) # just a program place indicator > puts '> logging started, at INFO level' > end > > end > > %w(rubygems ruby-debug readline strscan logger fileutils).each{ |lib| > require lib } > Debugger.start > debugger # call to ruby-debug > > main > > ==== > > Demonstration of the lack of access - > >> $ ruby setnet-x.rb >> setnet-x.rb:40 >> main >> (rdb:1) b 4 >> Breakpoint 1 file setnet-x.rb, line 4 >> (rdb:1) c >> Breakpoint 1 at setnet-x.rb:4 >> setnet-x.rb:4 >> run_log = Manage_log.new( 'logfile.txt' ).open >> (rdb:1) n >> > logging started, at INFO level >> setnet-x.rb:5 >> log = run_log.log >> (rdb:1) p run_log.log >> NoMethodError Exception: undefined method `log' for nil:NilClass >> (rdb:1) > I would truly appreciate someone's pointing out the problem here. > > Thanks! > > Tom > Take a look at ManageLog#open. The last expression is a call to puts, which returns nil. You are setting the variable "run_log" to the result of the call to open in the debugger, but that result is nil. Then you try to call "log" on nil, thus the error. -Justin