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

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Wed, Sep 15, 2010 at 4:46 PM, Gene Angelo <web.gma@gmail.com> wrote:

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

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