Simon Strandgaard <0bz63fz3m1qt3001 / sneakemail.com> wrote:
> In C++ private members is inacessable to derivatived classes.
> I want the same, but with a extra twist.
> Instances declared in the parent should be readonly(frozen) to the
> subclass.
> 
> So that the subclass (B):
> *  is allowed to read the variable.
> *  is disallowed to write to the variable (raise exception).

Offhand, I'd say it wasn't even hackable in pure ruby, because

1. variables are not first-class values in ruby
2. instance variables are always in scope in a class, so you can't do
something in a setter method, e.g., that prevents access.
3. assigning to a variable is simply syntax, so for instance, you could
trap a.a = 1, but not @a = 1.
4. even if you were willing to make @a an object, and access it via
@a.value and @a.value=, the message passed to @a wouldn't get the value
of 'self' from the object that sent the message, i.e. you can't use
accessor methods to enforce scoping. 

This is the closest I can think of at the moment:

  def a=(n)
    p "In class #{self.class}"
    if self.class == A
      @a = n
    else
      raise "protected!"
    end
  end

and all it'll protect you from is

class B < A; end
b = B.new
b.a #returns the value
b.a=1 #raises 'protected'

martin