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 :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
__


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

Hi –

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 ?

You wouldn’t – you’d have to roll your own. For example:

class A
attr_reader :thing
def thing=(val)
puts “OK, I am now setting @thing to #{val}!”
@thing = val
puts “Now I can do other stuff, if needed…”
end
end

a = A.new
a.thing = “blah”

Note that I’ve used attr_reader to save the trouble of writing the
getter method:

def thing
@thing
end

But I wrote the setter method by hand, because there was more going on
than attr_writer would have provided.

Another thing to keep in mind is that all of this is purely
conventional. You can write methods with ‘=’ in them that don’t set
anything:

class B
def thing=(val)
puts “Pointless use of ‘=’!”
end
end

b = B.new
b.thing = “nothing”

David

···

On Fri, 20 Sep 2002, Matthew, Graeme wrote:


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com