From: "Matthew, Graeme" Date: 2002-09-20T14:34:20+09:00 Subject: RE: Attribute Access Problems thanks for the help, i am willing to use the attr_reader, attr_writer options however the problem with these is that lets say you need to trigger of some events when an attribute is set, then how would you do it through attr_reader / writer ? I also thought the whole aim of def projectName=(val) was to act as a getter / setter attribute ? cheers Graeme Matthew Analyst Programmer Mercer Investment Consulting Level 29, 101 Collins Street, Melbourne, VIC, 3001, Australia Tel - 61 3 9245 5352 Fax - 61 3 9245 5330 visit http://www.merceric.com -----Original Message----- From: Lyle Johnson [mailto:lyle@users.sourceforge.net] Sent: Friday, 20 September 2002 15:21 To: ruby-talk@ruby-lang.org Subject: Re: Attribute Access Problems Matthew, Graeme wrote: > I'm just not getting this! Don't Panic! > What am I doing wrong here, this is taking way to long :-( > > c:/dev/cgi-bin/Project.rb:80:in `DisplayProject': undefined method > `projectName' for # (NameError) OK, I know you can read, but just to clarify: this error message is telling you that you're trying to call an instance method named "projectName" for the "Project" class, but there is no instance method by that name. > I'm now using instance variables > > def projectName=(val) > @projectName = val if val > return @projectName > end This looks fine, but keep in mind that "projectName=()" (a "setter" method) and "projectName()" (a "getter" method) are *two* different method names... > but it's failing here in my document: > > Name value="#{self.projectName}"> Although we can't see all of the code for your "Project" class, I'm guessing that you didn't define a "projectName" accessor method to complement the "projectName=" method. A quick way to do this (since you also have a "@projectName" instance variable) is to use the attr_reader method: class Project attr_reader :projectName end A more verbose (but otherwise equivalent) way to do it is to explicitly code-up the method: class Project def projectName @projectName end end If you don't already have "Programming Ruby" (a.k.a. the "Pickaxe" book) I'd recommend you pick up a copy. It's also available for free online. The second chapter: http://www.rubycentral.com/book/tut_classes.html has a section entitled "Objects and Attributes" that does a very nice job of explaining what's going on. Hope this helps, Lyle __ ********************************************** This e-mail and any attachments may be confidential or legally privileged. If you received this message in error or are not the intended recipient, you should destroy the e-mail message and any attachments or copies, and you are prohibited from retaining, distributing, disclosing or using any information contained herein. Please inform us of the erroneous delivery by return e-mail. Thank you for your cooperation. ********************************************** ec03/04