# Instance variables available in derived clases

**URL:** https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858
**Category:** ruby-talk
**Created:** [27 January 2007 11:28 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858 "2007-01-27T11:28:46Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Richard\_Roberts](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Richard\_Roberts](https://rubytalk.org/u/Richard_Roberts)
#### Post date: [27 January 2007 11:28 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/1 "2007-01-27T11:28:46Z")

</div>

Hi.

Please read my \<a  
href="[http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-derived.html](http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-derived.html)"\>blog  
post\</a\>, regarding instance variables in derived classes.

Does anyone know a way to make instance variables 'private'?

Cheers,  
Rich.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Richard\_Roberts](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Richard\_Roberts](https://rubytalk.org/u/Richard_Roberts)
#### Post date: [27 January 2007 11:30 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/2 "2007-01-27T11:30:44Z")

</div>

Ah - I see putting html into the posts doesn't work! The blog url is

> **[ruby instance variables in derived classes](http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-derived.html)**
>
> I thought I would tell the world about something in the ruby language which surprised me a bit. Anyone who has used ruby for any length of t...

🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Wolfgang\_Nadasi-Don1](https://avatars.discourse-cdn.com/v4/letter/w/958977/32.png) [@Wolfgang\_Nadasi-Don1](https://rubytalk.org/u/Wolfgang_Nadasi-Don1)
#### Post date: [27 January 2007 11:55 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/3 "2007-01-27T11:55:08Z")

</div>

Richard Roberts schrieb:

> Hi.
> 
> Please read my \<a  
> href="[http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-derived.html&quot;&gt;blog](http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-derived.html&quot;&gt;blog)  
> post\</a\>, regarding instance variables in derived classes.
> 
> Does anyone know a way to make instance variables 'private'?
> 
> Cheers,  
> Rich.

Instance variables are part of an object, they are the attributes that contain  
values special for each instance.

From an OO point of view a derived class can only extend an excisting class. This means all objects of the parent class are still relevant for the child, plus additional ones.

So it doesn't make any sense to hide these instance variables.

For local working inside a method "local variables" are available.

btw - There is one method in Ruby which I sometimes use for special cases, and which breakes the rule, that a derived class should only extend a class, an not  
shorten it's capabilities: "Module#undef\_method".

Wolfgang Nádasi-Donner

---

<div class="post-metadata">

### Author: ![Chris\_Carter](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Chris\_Carter](https://rubytalk.org/u/Chris_Carter)
#### Post date: [27 January 2007 20:53 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/4 "2007-01-27T20:53:44Z")

</div>

Richard,  
Instance variables are private, as you noticed you had to give it a  
getter/setter. What is happening the subclass inherits teh setter  
method, and when it is called, it operates on the object it was called  
on, not the object it was defined in, because for all intents and  
purposes, it was defined in the child class.

> **···**
>
> On 1/27/07, Richard Roberts \<ricisbest@yahoo.co.uk\> wrote:
> 
> > Hi.
> > 
> > Please read my \<a  
> > href="[http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-derived.html&quot;&gt;blog](http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-derived.html&quot;&gt;blog)  
> > post\</a\>, regarding instance variables in derived classes.
> > 
> > Does anyone know a way to make instance variables 'private'?
> > 
> > Cheers,  
> > Rich.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Chris Carter  
> concentrationstudios.com  
> brynmawrcs.com

---

<div class="post-metadata">

### Author: ![Richard\_Roberts](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Richard\_Roberts](https://rubytalk.org/u/Richard_Roberts)
#### Post date: [27 January 2007 12:08 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/5 "2007-01-27T12:08:09Z")

</div>

Hi Wolfgang. Thanks for your response.

I agree in many cases derived classes just extend an existing class, and  
all objects of the parent class are relevent to the children. In fact,  
I have often written quite a base class quite defensively in C#, only to  
find that I do indeed need to expose further properties to the derived  
ones later.

However, sometimes it is good to keep stuff in the base class private.  
What if there was an algorithm in the base class which depended on a  
class level instance variable to govern its behaviour? In ruby, all  
derived classes would be allowed to change this variable, possibly  
breaking or affecting it's operation (especially if the derived class  
was written by a different developer).

In rails, you can set instance variables in the ApplicationController,  
and they're automatically available in other controllers. The behaviour  
of ruby I described means that in the other controllers you've got to be  
careful not to accidentally overwrite this value.

i.e. We have not explicitly exposed the instance variable in the base  
class but in the derived classes we still need to know about the  
implementation of the base class. This kind of breaks the orthogonality  
of the design, doesn't it?

Rich.

Wolfgang Nádasi-donner wrote:

> **···**
>
> > Richard Roberts schrieb:
> > 
> > > 
> > 
> > Instance variables are part of an object, they are the attributes that  
> > contain  
> > values special for each instance.
> > 
> > From an OO point of view a derived class can only extend an excisting  
> > class.  
> > This means all objects of the parent class are still relevant for the  
> > child,  
> > plus additional ones.
> > 
> > So it doesn't make any sense to hide these instance variables.
> > 
> > For local working inside a method "local variables" are available.
> > 
> > btw - There is one method in Ruby which I sometimes use for special  
> > cases, and  
> > which breakes the rule, that a derived class should only extend a class,  
> > an not  
> > shorten it's capabilities: "Module#undef\_method".
> > 
> > Wolfgang Nádasi-Donner
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Richard\_Roberts](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Richard\_Roberts](https://rubytalk.org/u/Richard_Roberts)
#### Post date: [28 January 2007 00:05 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/6 "2007-01-28T00:05:52Z")

</div>

Chris, what you say about the method operating on the child class is  
true.

However, instance variables are not private (at least not in the  
C++/C#/java sense) because derived classes can access them. If they  
were, you wouldn't be able to access them outside the class in which  
they were defined.

The example in my blog post  
([http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-derived.html\](http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-derived.html%5C))

could be written like somehting this in C# (please forgive any typos, as  
I've not got a C# compiler on the computer on which I'm typing this)...

public Class Parent  
{  
&nbsp;&nbsp;private String var = String.Empty;

&nbsp;&nbsp;public String ParentMeth()  
&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;return "hello";  
&nbsp;&nbsp;}  
}

public Class Child:Parent  
{  
&nbsp;&nbsp;public String ChildMeth  
&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;return var;  
&nbsp;&nbsp;}  
}

...This shouldn't even compile, as var is not defined in the child.

Chris Carter wrote:

> **···**
>
> > On 1/27/07, Richard Roberts \<ricisbest@yahoo.co.uk\> wrote:
> > 
> > > --  
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > 
> > Richard,  
> > Instance variables are private, as you noticed you had to give it a  
> > getter/setter. What is happening the subclass inherits teh setter  
> > method, and when it is called, it operates on the object it was called  
> > on, not the object it was defined in, because for all intents and  
> > purposes, it was defined in the child class.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Richard\_Roberts](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Richard\_Roberts](https://rubytalk.org/u/Richard_Roberts)
#### Post date: [27 January 2007 16:05 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/7 "2007-01-27T16:05:46Z")

</div>

Anyone else got views on this??

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Richard\_Roberts](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Richard\_Roberts](https://rubytalk.org/u/Richard_Roberts)
#### Post date: [28 January 2007 00:08 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/8 "2007-01-28T00:08:27Z")

</div>

Sorry - the code in my last entry should read...

public Class Parent  
{  
&nbsp;&nbsp;private String var = String.Empty;

&nbsp;&nbsp;public String ParentMeth()  
&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;var = "hello";  
&nbsp;&nbsp;&nbsp;&nbsp;return var;  
&nbsp;&nbsp;}  
}

public Class Child:Parent  
{  
&nbsp;&nbsp;public String ChildMeth  
&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;return var;  
&nbsp;&nbsp;}  
}

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)
#### Post date: [27 January 2007 16:20 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/9 "2007-01-27T16:20:12Z")

</div>

It looks like Ruby 1.9 will give you the ability to define private  
methods in the base class (which may be simple getter/setter methods)  
that are accessible to the base class, but not the descendents. Of  
course the subclass will still have access to the instance variables -  
in my opinion it would be madness to have 'inheritance' without this.

> **···**
>
> On Jan 27, 9:05 am, Richard Roberts \<ricisb...@yahoo.co.uk\> wrote:
> 
> > Anyone else got views on this??

---

<div class="post-metadata">

### Author: ![Richard\_Roberts](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Richard\_Roberts](https://rubytalk.org/u/Richard_Roberts)
#### Post date: [28 January 2007 00:10 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/10 "2007-01-28T00:10:47Z")

</div>

Anyway, looks like it's just the way it is in ruby, and I'm going to  
have to be careful when I inherit.

Thanks, all.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Avdi\_Grimm1](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Avdi\_Grimm1](https://rubytalk.org/u/Avdi_Grimm1)
#### Post date: [27 January 2007 19:43 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/11 "2007-01-27T19:43:31Z")

</div>

To the contrary, it's not madness at all. It's a legitimate  
interpretation of OO principles to say that a private member variable  
is private to a class, and should be inaccessible to all clients of  
the class - INCLUDING derived classes. Because your class may be used  
as a base class by other programmers in other projects, it's important  
to be able to define a stable interface not only to the class's  
collaborators (other classes), but to it's children as well. That way  
other programmers can build on your class without worrying about the  
changes in implementation.

This is precisely why both C++ and Java make a distinction between  
"private" and "protected". The former is for internal use only by the  
class; the latter is the interface the class exposes to it's  
descendants. And public, of course, is the interface it exposes to  
the outside world.

Actually, I wouldn't mind seeing this in Ruby. Ruby's approach is  
very flexible and convenient, but it carries with it the danger that  
if you derive from a third-party class (like, say, ActiveRecord), you  
might inadvertently overwrite a member variable that the base class  
uses.

> **···**
>
> --  
> Avdi
> 
> On 1/27/07, Phrogz \<gavin@refinery.com\> wrote:
> 
> > On Jan 27, 9:05 am, Richard Roberts \<ricisb...@yahoo.co.uk\> wrote:  
> > \> Anyone else got views on this??
> > 
> > It looks like Ruby 1.9 will give you the ability to define private  
> > methods in the base class (which may be simple getter/setter methods)  
> > that are accessible to the base class, but not the descendents. Of  
> > course the subclass will still have access to the instance variables -  
> > in my opinion it would be madness to have 'inheritance' without this.

---

<div class="post-metadata">

### Author: ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)
#### Post date: [29 January 2007 01:34 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/12 "2007-01-29T01:34:16Z")

</div>

Bertrand Meyer in Object Oriented Software Engineering talks a lot  
about the differences between the client and subclass relationships.  
If I recall, he argues that a choice to subclass is in effect a choice  
to reuse implementation.

Delegation is another way to reuse a class without gaining access to  
private information.

Gary Wright

> **···**
>
> On Jan 27, 2007, at 7:10 PM, Richard Roberts wrote:
> 
> > Anyway, looks like it's just the way it is in ruby, and I'm going to  
> > have to be careful when I inherit.

---

<div class="post-metadata">

### Author: ![Richard\_Roberts](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@Richard\_Roberts](https://rubytalk.org/u/Richard_Roberts)
#### Post date: [27 January 2007 19:50 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/13 "2007-01-27T19:50:52Z")

</div>

I'm glad not everyone thinks I'm mad!

Does anyone know a good way to simulate the C++ and Java behaviour in  
ruby? I've tried various things, but to no avail (e.g. declaring them as  
class-level instance variables in a private block, creating private  
accessors etc).

Avdi Grimm wrote:  
"\> To the contrary, it's not madness at all"

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Wolfgang\_Nadasi-Don1](https://avatars.discourse-cdn.com/v4/letter/w/958977/32.png) [@Wolfgang\_Nadasi-Don1](https://rubytalk.org/u/Wolfgang_Nadasi-Don1)
#### Post date: [27 January 2007 20:40 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/14 "2007-01-27T20:40:09Z")

</div>

Avdi Grimm schrieb:

> To the contrary, it's not madness at all. It's a legitimate  
> interpretation of OO principles to say that a private member variable  
> is private to a class, and should be inaccessible to all clients of  
> the class - INCLUDING derived classes.

I think this not possible in Ruby in general, because onybody can change a class later.

It's normal practice to write code in Ruby, which changes classes of the standard library.

\>\>\>\>\> Example \>\>\>\>\>

class Otto  
&nbsp;&nbsp;&nbsp;def show  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "here is 'show'"  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;private  
&nbsp;&nbsp;&nbsp;def hiddenshow  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "here is 'hiddenshow'"  
&nbsp;&nbsp;&nbsp;end  
end

o = Otto.new  
o.show  
o.hiddenshow rescue puts '+++ cannot call hiddenshow'

class Otto  
&nbsp;&nbsp;&nbsp;public :hiddenshow  
end

o.hiddenshow

\>\>\>\>\> Output \>\>\>\>\>

here is 'show'  
+++ cannot call hiddenshow  
here is 'hiddenshow'

\>\>\>\>\> EoE \>\>\>\>\>

Wolfgang Nádasi-Donner

---

<div class="post-metadata">

### Author: ![Vincent\_Fourmond](https://avatars.discourse-cdn.com/v4/letter/v/cdc98d/32.png) [@Vincent\_Fourmond](https://rubytalk.org/u/Vincent_Fourmond)
#### Post date: [27 January 2007 20:11 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/15 "2007-01-27T20:11:21Z")

</div>

Richard Roberts wrote:

> I'm glad not everyone thinks I'm mad!
> 
> Does anyone know a good way to simulate the C++ and Java behaviour in  
> ruby? I've tried various things, but to no avail (e.g. declaring them as  
> class-level instance variables in a private block, creating private  
> accessors etc).

&nbsp;&nbsp;Write a C structure holding your variables (it doesn't even have to be  
complex: a VALUE array should do). Apart from that, no solution. You  
won't change easily a design feature of Ruby ;-)...

&nbsp;&nbsp;Vince

> **···**
>
> --  
> Vincent Fourmond, PhD student (not for long anymore)  
> [http://vincent.fourmond.neuf.fr/](http://vincent.fourmond.neuf.fr/)

---

<div class="post-metadata">

### Author: ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)
#### Post date: [28 January 2007 01:29 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/16 "2007-01-28T01:29:24Z")

</div>

Use define\_method against local vars.

&nbsp;&nbsp;class X  
&nbsp;&nbsp;&nbsp;&nbsp;x = 100

&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:x) do ; x; end  
&nbsp;&nbsp;end

&nbsp;&nbsp;X.new.x #=\> 10

Generalizing this, perhaps make use an OpenStruct (or Facets'  
OpenObject class) and use that for local vars:

&nbsp;&nbsp;class X  
&nbsp;&nbsp;&nbsp;&nbsp;local = OpenObject.new  
&nbsp;&nbsp;&nbsp;&nbsp;local.x = 10

&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:x) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local.x  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

Personally I think Ruby would do well to make it's method defintion  
syntax uniform across def and define\_method. Instead it could control  
scope via alternate forms of 'do'. In other words, let 'def' be a  
method too, or at least a short hand that actually calls  
define\_method. It would be nice to write:

&nbsp;&nbsp;class X  
&nbsp;&nbsp;&nbsp;&nbsp;local = OpenObject.new  
&nbsp;&nbsp;&nbsp;&nbsp;local.x = 10

&nbsp;&nbsp;&nbsp;&nbsp;def x does  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local.x  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

Where 'does' keeps the scope open.

T.

> **···**
>
> On Jan 27, 2:50 pm, Richard Roberts \<ricisb...@yahoo.co.uk\> wrote:
> 
> > I'm glad not everyone thinks I'm mad!
> > 
> > Does anyone know a good way to simulate the C++ and Java behaviour in  
> > ruby? I've tried various things, but to no avail (e.g. declaring them as  
> > class-level instance variables in a private block, creating private  
> > accessors etc).

---

<div class="post-metadata">

### Author: ![Avdi\_Grimm1](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Avdi\_Grimm1](https://rubytalk.org/u/Avdi_Grimm1)
#### Post date: [28 January 2007 21:07 UTC](https://rubytalk.org/t/instance-variables-available-in-derived-clases/34858/17 "2007-01-28T21:07:22Z")

</div>

Whoa! That's cool!

I love how no matter how long I program in Ruby, I still get pleasant  
surprised like this.

Thanks!

> **···**
>
> On 1/27/07, Trans \<transfire@gmail.com\> wrote:
> 
> > Use define\_method against local vars.
> > 
> > &nbsp;&nbsp;class X  
> > &nbsp;&nbsp;&nbsp;&nbsp;x = 100
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;define\_method(:x) do ; x; end  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;X.new.x #=\> 10
> 
> --  
> Avdi
