# "Declaring" private instance variables

**URL:** <https://www.ruby-forum.com/t/declaring-private-instance-variables/61027>\
**Category:** Ruby\
**Created:** [May 19, 2006, 12:15pm UTC](https://www.ruby-forum.com/t/declaring-private-instance-variables/61027 "2006-05-19T12:15:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2006, 12:15pm UTC](https://www.ruby-forum.com/t/declaring-private-instance-variables/61027/1 "2006-05-19T12:15:24Z")

</div>

Hello fellow Rybuists,

One thing I like about C++'s OO is the way in which all class & instance  
variables are conveniently “declared” in respective public and private  
sections. This makes the code easier to understand, since the  
declaration is a good place to comment on the variable, explaining how  
it is used.

Ruby has several options regarding this, but nothing of a concensus, it  
seems. Public members can be declared with ‘attr’ and friends. But what  
about private memberes. For now I find it convenient to preallocate  
those in ‘initialize’. What are the approaches you use ?

My aim is to enhance the readability of my class. It won’t affect its  
usage, just its comprehension when I return in the future trying to grok  
how it works.

Eli

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2006, 12:30pm UTC](https://www.ruby-forum.com/t/declaring-private-instance-variables/61027/2 "2006-05-19T12:30:38Z")

</div>

On 5/19/06, Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> about private memberes. For now I find it convenient to preallocate

Actually, the attr-family is just some metaprogramming to create new  
methods. There are no public instance variables.

Setting your variables up in initialize is fine, or when they’re first  
used. Doesn’t really matter.

Pat

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2006, 12:33pm UTC](https://www.ruby-forum.com/t/declaring-private-instance-variables/61027/3 "2006-05-19T12:33:47Z")

</div>

Hi –

On Fri, 19 May 2006, Eli B. wrote:

> about private memberes. For now I find it convenient to preallocate  
> those in ‘initialize’. What are the approaches you use ?

The behaviors provided by attr\_\* are implemented using instance  
variables, but that’s not the whole instance variable story. I’m not  
quite sure which constructs you’re specifically thinking of, but if  
you want your attr\_\* methods private, just do:

class C  
private  
attr\_accessor :x

```
 # etc.

```

end

The privacy you’re gaining here is the privacy of the x and x=  
methods. That’s separate from the fact that @x is “private” (i.e.,  
only visible to the instance whose variable it is). Even non-private  
attr\_\*-generated methods use instance variables. (Usually the point  
is to make the object’s state _less_ private 🙂

You can always just use an instance variable on its own, too.

> My aim is to enhance the readability of my class. It won’t affect its  
> usage, just its comprehension when I return in the future trying to grok  
> how it works.

I don’t think you’ll get much clearer than:

@n = 1

🙂

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2006, 12:47pm UTC](https://www.ruby-forum.com/t/declaring-private-instance-variables/61027/4 "2006-05-19T12:47:42Z")

</div>

> The behaviors provided by attr\_\* are implemented using instance  
> variables, but that’s not the whole instance variable story. I’m not  
> quite sure which constructs you’re specifically thinking of, but if  
> you want your attr\_\* methods private, just do:
> 
> class C  
> private  
> attr\_accessor :x
> 
> ```
> # etc.
> 
> ```
> 
> end
> 
> The privacy you’re gaining here is the privacy of the x and x=  
> methods. That’s separate from the fact that @x is “private” (i.e.,  
> only visible to the instance whose variable it is). Even non-private  
> attr\_\*-generated methods use instance variables. (Usually the point  
> is to make the object’s state _less_ private 🙂 too.

Interesting - I didn’t know that it’s possible to have private variables  
by means of attr\_\* in ‘private’. Thanks

> > My aim is to enhance the readability of my class. It won’t affect its  
> > usage, just its comprehension when I return in the future trying to grok  
> > how it works.
> 
> I don’t think you’ll get much clearer than:
> 
> @n = 1

Sure, the question is where to put this line. I find that when such  
“declarations” are interspersed in various methods of a 300-line class,  
it makes comprehension more difficult.

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/declaring-private-instance-variables/61027/5 "2006-05-19T12:54:56Z")

</div>

Hi –

On Fri, 19 May 2006, Eli B. wrote:

> > ```
> > # etc.
> > 
> > ```
> > 
> > end
> > 
> > The privacy you’re gaining here is the privacy of the x and x=  
> > methods. That’s separate from the fact that @x is “private” (i.e.,  
> > only visible to the instance whose variable it is). Even non-private  
> > attr\_\*-generated methods use instance variables. (Usually the point  
> > is to make the object’s state _less_ private 🙂 too.
> 
> Interesting - I didn’t know that it’s possible to have private variables  
> by means of attr\_\* in ‘private’. Thanks

You’re actually getting private methods, not variables. The methods  
happen to use instance variables internally, but that’s a separate  
matter from their access level.

> “declarations” are interspersed in various methods of a 300-line class,  
> it makes comprehension more difficult.

It’s not really a declaration, though; it’s just a variable  
assignment. Just put it somewhere before you need the variable to  
have that value 🙂

David

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/declaring-private-instance-variables/61027/6 "2006-05-19T14:47:01Z")

</div>

Eli B. wrote:

> Hello fellow Rybuists,
> 
> One thing I like about C++'s OO is the way in which all class & instance  
> variables are conveniently “declared” in respective public and private  
> sections. This makes the code easier to understand, since the  
> declaration is a good place to comment on the variable, explaining how  
> it is used.
> 
> Ruby has several options regarding this, but nothing of a concensus, it  
> seems. Public members can be declared with ‘attr’ and friends. But what  
> about private memberes. For now I find it convenient to preallocate  
> those in ‘initialize’. What are the approaches you use ?
> 
> My aim is to enhance the readability of my class. It won’t affect its  
> usage, just its comprehension when I return in the future trying to grok  
> how it works.

IMHO, declaration of private intance variable is a deep-rooted static  
way of thinking. But ruby is dynamic, in ruby, I think we need think  
dynamically. So, acctually, we don’t need any instance variable  
declaration. Any instance variable can be added at the time you need.  
That’s ruby’s way.

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2006, 3:53pm UTC](https://www.ruby-forum.com/t/declaring-private-instance-variables/61027/7 "2006-05-19T15:53:04Z")

</div>

On May 19, 2006, at 6:31 AM, removed\_email\_address@domain.invalid wrote:

> end  
> Is there any existing (or previously proposed) way of ‘scoping’ the  
> behavior  
> of private? I’m thinking something like:

class A  
attr\_accessor :a  
private {  
attr\_accessor :b  
}  
end

I’m aware that you can ‘toggle’ visibility:

class A  
attr\_accessor :a # public  
private  
attr\_accessor :b # private  
public  
attr\_accessor :c # public  
end

but it would seem some sort of block/scope notation would be cleaner.

Gary W.

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/declaring-private-instance-variables/61027/8 "2006-05-19T16:05:32Z")

</div>

On 5/19/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but it would seem some sort of block/scope notation would be cleaner.  
> No, there isn’t. Why would you want that? I see no advantage to it,  
> and it’s odd. I prefer this, though:

class A  
attr\_accessor :a, :b, :c  
private :b, :b=  
end

-austin

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/declaring-private-instance-variables/61027/9 "2006-05-19T18:45:57Z")

</div>

unknown wrote:

> > Interesting - I didn’t know that it’s possible to have private variables  
> > by means of attr\_\* in ‘private’. Thanks
> 
> You’re actually getting private methods, not variables. The methods  
> happen to use instance variables internally, but that’s a separate  
> matter from their access level.

I also get a warning on this:

78: warning: private attribute?

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2006, 4:29pm UTC](https://www.ruby-forum.com/t/declaring-private-instance-variables/61027/10 "2006-05-19T16:29:58Z")

</div>

On May 19, 2006, at 10:03 AM, Austin Z. wrote:

> No, there isn’t. Why would you want that? I see no advantage to it,  
> and it’s odd. I prefer this, though:
> 
> class A  
> attr\_accessor :a, :b, :c  
> private :b, :b=  
> end

Seems like a lot of redundancy there. You are typing :b three times.  
What if you had three public attributes and three private?

class A  
attr\_accessor :a, :b, :c, :d, :e, :f  
private :d, :d=, :e, :e=, :f, :f=  
end

vs.

class A  
attr\_accessor :a, :b, :c  
private { attr\_accessor :d, :e, :f }  
end

The hidden state behavior of public, private, protected (changing the  
default visibility of method definitions) always seems a little  
awkward to me.  
When they are used with arguments it doesn’t seem as awkward.

Gary W.

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 19, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/declaring-private-instance-variables/61027/11 "2006-05-19T23:36:24Z")

</div>

On May 19, 2006, at 10:26 AM, removed\_email\_address@domain.invalid wrote:

> class A  
> Gary W.

Here’s an implementation. I almost laughed at myself when i figured  
out the right way to do it, trying to use all sorts of  
Binding.of\_caller type magic to get the toggle methods to work  
properly, then I realized the correct way to do it.

% cat block\_private.rb  
class Module  
def private\_body(&block)  
old\_meths = instance\_methods  
class\_eval(&block)  
new\_meths = instance\_methods - old\_meths  
new\_meths.each do |meth|  
private(meth)  
end  
nil  
end  
end

class A  
def pub\_meth  
puts “This is fine”  
end

private\_body do  
def priv\_meth  
puts “Can’t call me.”  
end  
end

def pub\_meth2  
puts “Yay!”  
end  
end

a = A.new  
a.pub\_meth  
a.pub\_meth2  
a.priv\_meth

% ruby block\_private.rb  
-:32: private method `priv\_meth’ called for #\<A:0x1e82fc\>  
(NoMethodError)  
This is fine  
Yay!

The misordered output is an artifact of my shiny new script for doing  
examples.
