From: Tom Cloyd Date: 2009-01-21T12:01:44+09:00 Subject: Re: cannot get attr_accessor to work David A. Black wrote: > Hi -- > > On Wed, 21 Jan 2009, Tom Cloyd wrote: > >> David A. Black wrote: >>> Hi -- >>> >>> On Wed, 21 Jan 2009, 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 >>> >>> 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 :-) > > 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 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~