Property short cut for read-ony instance access, but

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=
#…

On Wed, Sep 15, 2010 at 4:46 PM, Gene A. [email protected] wrote:

or am I doomed to do it this way…
#…
With a little meta programming you can do

class Module
def attr_protected *names
attr_accessor *names
protected *names.map {|n| “#{n}=”}
end
end

and use it like

class A
attr_protected :foo, :bar
end

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs