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
> =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/