Cannot get attr_accessor to work

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

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hi --

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

I haven't read the rest of your code but I'll bet the problem is right
there. Ruby is interpreting log and logging_now as local variables. If
you want to call the methods of those names, you have to do:

   self.log = run_log.log
   self.logging_now = run_log.lgg

Basically, given any expression that looks like this:

    var = value

the parser interprets var as a local variable name. So you have to add
the explicit receiver to achieve the method call.

David

···

On Wed, 21 Jan 2009, Tom Cloyd wrote:

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2\)

http://www.wishsight.com => Independent, social wishlist management!

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

David A. Black wrote:

Hi --

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

I haven't read the rest of your code but I'll bet the problem is right
there. Ruby is interpreting log and logging_now as local variables. If
you want to call the methods of those names, you have to do:

  self.log = run_log.log
  self.logging_now = run_log.lgg

Basically, given any expression that looks like this:

   var = value

the parser interprets var as a local variable name. So you have to add
the explicit receiver to achieve the method call.

David

David,

Thank for your reply, but I'm puzzled by it. Doesn't the code in my previous post make it clear that I'm dealing with instance variables? The class instance initiates them, e.g., @log, and I'm trying to get that with the run_log.log call. This is NOT an attempt to call a method. I very carefully copied (I thought) the pattern I saw in several authoritative sources, but it doesn't work for me, which is nuts.

I hope this makes sense.

t.

···

On Wed, 21 Jan 2009, Tom Cloyd wrote:

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Justin Collins wrote:

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

Well, nuts. That's crystal clear. Man, it's tough being a Ruby amateur, when it 's not great fun, which it hasn't been for a couple of hours.

Thanks Justin. Much appreciated.

t.

···

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Tom Cloyd wrote:

Justin Collins wrote:

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 -

<snip>

(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

Well, nuts. That's crystal clear. Man, it's tough being a Ruby amateur, when it 's not great fun, which it hasn't been for a couple of hours.

Thanks Justin. Much appreciated.

t.

Just remember that whenever you see

NoMethodError Exception: undefined method `..." for nil:NilClass

look at what variable you are calling the method on. Then figure out why it is nil.

-Justin

Hi --

David A. Black wrote:

Hi --

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

I haven't read the rest of your code but I'll bet the problem is right
there. Ruby is interpreting log and logging_now as local variables. If
you want to call the methods of those names, you have to do:

  self.log = run_log.log
  self.logging_now = run_log.lgg

Basically, given any expression that looks like this:

   var = value

the parser interprets var as a local variable name. So you have to add
the explicit receiver to achieve the method call.

David

David,

Thank for your reply, but I'm puzzled by it. Doesn't the code in my previous post make it clear that I'm dealing with instance variables? The class instance initiates them, e.g., @log, and I'm trying to get that with the run_log.log call. This is NOT an attempt to call a method. I very carefully copied (I thought) the pattern I saw in several authoritative sources, but it doesn't work for me, which is nuts.

I hope this makes sense.

The code I saw was:

run_log = Manage_log.new( 'logfile.txt' ).open
log = run_log.log
logging_now = run_log.lgg # @logging_now = false

which are local variable assignments, and you'd mentioned
attr_accessor, so I assumed you had attr_accessor somewhere in a part
of the code you hadn't posted. It's a common error to do:

   class C
     attr_accessor :name
     def initialize(name)
       name = name # should be @name or self.name
     end
   end

so I surmised that that was what was going on. Oh well -- can't hurt
to see that particular potential problem anyway :slight_smile:

I'm still not sure where attr_accessor fits in.

David

···

On Wed, 21 Jan 2009, Tom Cloyd wrote:

On Wed, 21 Jan 2009, Tom Cloyd wrote:

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2\)

http://www.wishsight.com => Independent, social wishlist management!

David A. Black wrote:

Hi --

David A. Black wrote:

Hi --

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

I haven't read the rest of your code but I'll bet the problem is right
there. Ruby is interpreting log and logging_now as local variables. If
you want to call the methods of those names, you have to do:

  self.log = run_log.log
  self.logging_now = run_log.lgg

Basically, given any expression that looks like this:

   var = value

the parser interprets var as a local variable name. So you have to add
the explicit receiver to achieve the method call.

David

David,

Thank for your reply, but I'm puzzled by it. Doesn't the code in my previous post make it clear that I'm dealing with instance variables? The class instance initiates them, e.g., @log, and I'm trying to get that with the run_log.log call. This is NOT an attempt to call a method. I very carefully copied (I thought) the pattern I saw in several authoritative sources, but it doesn't work for me, which is nuts.

I hope this makes sense.

The code I saw was:

run_log = Manage_log.new( 'logfile.txt' ).open
log = run_log.log
logging_now = run_log.lgg # @logging_now = false

which are local variable assignments, and you'd mentioned
attr_accessor, so I assumed you had attr_accessor somewhere in a part
of the code you hadn't posted. It's a common error to do:

  class C
    attr_accessor :name
    def initialize(name)
      name = name # should be @name or self.name
    end
  end

so I surmised that that was what was going on. Oh well -- can't hurt
to see that particular potential problem anyway :slight_smile:

I'm still not sure where attr_accessor fits in.

David

Sorry you got a partial copy of the code I sent. It rather sounded like that was what happened. I DO appreciate that you responded so quickly. You've certainly been very helpful to me on a number of occasions. (And I require that help, at times, if I'm get anything much accomplished in Ruby, in the time I have!).

Thanks.

Tom

···

On Wed, 21 Jan 2009, Tom Cloyd wrote:

On Wed, 21 Jan 2009, Tom Cloyd wrote:

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Justin Collins wrote:

Tom Cloyd wrote:

Justin Collins wrote:

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 -

<snip>

(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

Well, nuts. That's crystal clear. Man, it's tough being a Ruby amateur, when it 's not great fun, which it hasn't been for a couple of hours.

Thanks Justin. Much appreciated.

t.

Just remember that whenever you see

NoMethodError Exception: undefined method `..." for nil:NilClass

look at what variable you are calling the method on. Then figure out why it is nil.

-Justin

Got it! Makes total sense (in retrospect).

Tks,

t.

···

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hi --

David A. Black wrote:

Hi --

David A. Black wrote:

Hi --

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

I haven't read the rest of your code but I'll bet the problem is right
there. Ruby is interpreting log and logging_now as local variables. If
you want to call the methods of those names, you have to do:

  self.log = run_log.log
  self.logging_now = run_log.lgg

Basically, given any expression that looks like this:

   var = value

the parser interprets var as a local variable name. So you have to add
the explicit receiver to achieve the method call.

David

David,

Thank for your reply, but I'm puzzled by it. Doesn't the code in my previous post make it clear that I'm dealing with instance variables? The class instance initiates them, e.g., @log, and I'm trying to get that with the run_log.log call. This is NOT an attempt to call a method. I very carefully copied (I thought) the pattern I saw in several authoritative sources, but it doesn't work for me, which is nuts.

I hope this makes sense.

The code I saw was:

run_log = Manage_log.new( 'logfile.txt' ).open
log = run_log.log
logging_now = run_log.lgg # @logging_now = false

which are local variable assignments, and you'd mentioned
attr_accessor, so I assumed you had attr_accessor somewhere in a part
of the code you hadn't posted. It's a common error to do:

  class C
    attr_accessor :name
    def initialize(name)
      name = name # should be @name or self.name
    end
  end

so I surmised that that was what was going on. Oh well -- can't hurt
to see that particular potential problem anyway :slight_smile:

I'm still not sure where attr_accessor fits in.

David

Sorry you got a partial copy of the code I sent. It rather sounded like that was what happened.

I think I got it all (the bit I quoted was just a few lines of it),
but I surmised too quickly what your problem was. I'm STILL not sure
where attr_accessor fits in :slight_smile: (You're not calling it anywhere, are
you?) But it sounds like you got the problem resolved.

I DO appreciate that you responded so quickly. You've certainly
been very helpful to me on a number of occasions. (And I require
that help, at times, if I'm get anything much accomplished in Ruby,
in the time I have!).

Glad to be of help!

David

···

On Wed, 21 Jan 2009, Tom Cloyd wrote:

On Wed, 21 Jan 2009, Tom Cloyd wrote:

On Wed, 21 Jan 2009, Tom Cloyd wrote:

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2\)

http://www.wishsight.com => Independent, social wishlist management!