Attribute Access Problems

Im just not getting this !

What am I doing wrong here, this is taking way to long :frowning:

c:/dev/cgi-bin/Project.rb:80:in DisplayProject': undefined methodprojectName’ for #Project:0x25dfae0 (NameError)

im now using instance variables

def projectName=(val)
@projectName = val if val
return @projectName
end

but its failing in my here document

Name

I have defined the method so it should return nil if the instance variable
has not been assigned a value and therefore the html should be interpolated
to

Name

aaaagh, maybe perl’s OO way is getting me into trouble here :slight_smile:

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

···

__


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

Matthew, Graeme wrote:

I’m just not getting this!

Don’t Panic!

What am I doing wrong here, this is taking way to long :frowning:

c:/dev/cgi-bin/Project.rb:80:in DisplayProject': undefined method projectName’ for #Project:0x25dfae0 (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

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

Im just not getting this !

What am I doing wrong here, this is taking way to long :frowning:

c:/dev/cgi-bin/Project.rb:80:in DisplayProject': undefined method projectName’ for #Project:0x25dfae0 (NameError)

This error message clearly says that there’s no method called
“projectName”.

im now using instance variables

def projectName=(val)
@projectName = val if val
return @projectName
end

Here you define the method “projectName=” (note the trailing =
character).

but its failing in my here document

Name

Here you call the method “projectName” (no = character).

HTH

Regards,
Pit

···

On 20 Sep 2002, at 13:18, Matthew, Graeme wrote: