From: "David A. Black" Date: 2009-01-21T11:47:51+09:00 Subject: Re: cannot get attr_accessor to work 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 -- 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!