Private Writer

Hi all, its been a long but educational day :slight_smile:

Question:

If I create reader and writer attributes

eg
attr_reader :code, :name, :telephone
attr_writer :name, :telephone

Now self.code is a system generated code but how can I set it privately for
example

I call the db and get the data in a hash

I can use

self.name = data['name']
self.telephone = data['telephone']

my problem is that I cannot set the code from the db (ie data[‘code’])
otherwise I would have to declare it in attr_writer which would make it
public, is there a way to make a writer attribute private eg

private

attr_writer :code

Many thanks

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

attr_reader :code
generates an instance variable
@code
as well as an accessor method, basically
def code
return @code
end

Internal to the class you can just access the variable directly
@code = …

BTW, attr_accessor creates a reader and writer on a given variable, so
you could write
attr_accessor :name, :telephone
attr_reader :code

Hope this helps.

Cheers,
alan

···

On Fri, Sep 20, 2002 at 02:54:56PM +0900, Matthew, Graeme wrote:

Hi all, its been a long but educational day :slight_smile:

Question:

If I create reader and writer attributes

eg
attr_reader :code, :name, :telephone
attr_writer :name, :telephone

Now self.code is a system generated code but how can I set it privately for
example


Alan Chen
Digikata LLC