property short cut for read-ony instance access, but....

G

Gene Angelo

Hello,

Is there a short cut for creating a property that has read-only access
from instance variables and read/write access for derived classes?

Was looking for something like...

attr_reader :prompt
attr_writer protected :prompt

or am I doomed to do it this way...

#...
def prompt
@prompt
end

def prompt=(value)
@prompt=value
end
protected :prompt=
#...
 
R

Robert Klemme

Hello,

Is there a short cut for creating a property that has read-only access
from instance variables and read/write access for derived classes?

Was looking for something like...

attr_reader :prompt
attr_writer protected :prompt

or am I doomed to do it this way...

#...
def prompt
=A0 @prompt
end

def prompt=3D(value)
=A0 @prompt=3Dvalue
end
protected :prompt=3D
#...

With a little meta programming you can do

class Module
def attr_protected *names
attr_accessor *names
protected *names.map {|n| "#{n}=3D"}
end
end

and use it like

class A
attr_protected :foo, :bar
end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top