# How to specify access modifers for variables?

**URL:** <https://www.ruby-forum.com/t/how-to-specify-access-modifers-for-variables/105685>\
**Category:** Ruby\
**Created:** [June 1, 2007, 11:02am UTC](https://www.ruby-forum.com/t/how-to-specify-access-modifers-for-variables/105685 "2007-06-01T11:02:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![raja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@raja](https://www.ruby-forum.com/u/raja)\
**Post date:** [June 1, 2007, 11:02am UTC](https://www.ruby-forum.com/t/how-to-specify-access-modifers-for-variables/105685/1 "2007-06-01T11:02:38Z")

</div>

I know this could be naive but still:

class Person  
attr\_reader :genre  
protected :genre  
def psedonym  
@name  
end  
end

In the class above, I can declare variable genre to be protected,  
but when I try to do the same for variable name, I get this error

another.rb:3:in `protected': undefined method`Person’ for class  
`Person’ (NameError)

And the same error props up when I comment the line #attr\_reader :genre

It appears that i have to provide accessors for the variables before  
declaring access modifiers;

and which in fact are “methods”, thus IS IT IMPOSSIBLE TO DECLARE ACCESS  
MODIFIERS FOR VARIABLES ?

Can Anyone Clarify??

Thanks  
Raja

---

<div class="post-metadata">

**Author:** ![raja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@raja](https://www.ruby-forum.com/u/raja)\
**Post date:** [June 1, 2007, 11:11am UTC](https://www.ruby-forum.com/t/how-to-specify-access-modifers-for-variables/105685/2 "2007-06-01T11:11:31Z")

</div>

Alle venerdÃ¬ 1 giugno 2007, raja ha scritto:

> thus IS IT IMPOSSIBLE TO DECLARE ACCESS  
> MODIFIERS FOR VARIABLES ?

Yes, it’s impossible. But you don’t need it. Instance variables are  
always  
private. You can’t do:

an\_object.@var

(you’ll get a SyntaxError exception if you try that)

So, if you want to access an object’s instance variable from outside the  
object, you need to provide a method which does that (note that all  
attr\_reader do is to define a method which returns the instance  
variable).  
After defining the method, you can make it protected or private, if you  
need  
to.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![raja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@raja](https://www.ruby-forum.com/u/raja)\
**Post date:** [June 1, 2007, 11:14am UTC](https://www.ruby-forum.com/t/how-to-specify-access-modifers-for-variables/105685/3 "2007-06-01T11:14:31Z")

</div>

On Friday 01 June 2007 11:02, raja wrote:

> In the class above, I can declare variable genre to be protected,  
> and which in fact are “methods”, thus IS IT IMPOSSIBLE TO DECLARE ACCESS  
> MODIFIERS FOR VARIABLES ?
> 
> Can Anyone Clarify??
> 
> Thanks  
> Raja

What you have to do is:

class Person

# you can define

public

# , but stuff is public by default when it hasn’t been specified yet.

belongs\_to(bla)  
validates\_presence\_of(hohoho)

protected

attr\_reader(:genre)

# As example; validate needs to be protected.

def validate

end

private

def private\_method

end  
end

---

<div class="post-metadata">

**Author:** ![raja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@raja](https://www.ruby-forum.com/u/raja)\
**Post date:** [June 1, 2007, 11:22am UTC](https://www.ruby-forum.com/t/how-to-specify-access-modifers-for-variables/105685/4 "2007-06-01T11:22:33Z")

</div>

On Friday 01 June 2007 11:13, Wiebe C. wrote:

> stuff

Never mind, I misunderstood the question… I thought you declared the  
method  
as parameter of protected, but you use it to set the visibility of an  
already  
existing method… Answering questions when you’re just awake doesn’t  
work…

---

<div class="post-metadata">

**Author:** ![raja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@raja](https://www.ruby-forum.com/u/raja)\
**Post date:** [June 1, 2007, 11:26am UTC](https://www.ruby-forum.com/t/how-to-specify-access-modifers-for-variables/105685/5 "2007-06-01T11:26:03Z")

</div>

Stefano C. wrote:

> Yes, it’s impossible. But you don’t need it. Instance variables are  
> always  
> private. You can’t do:  
> …  
> …  
> I hope this helps
> 
> Stefano

yup it helps,  
but coming from Java background that was what I expected!

Anyway though Thanks

And yes I had heard about the quick ruby response,  
and now for the first day of my learning spree, I have experienced it  
too

---

<div class="post-metadata">

**Author:** ![raja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@raja](https://www.ruby-forum.com/u/raja)\
**Post date:** [June 1, 2007, 11:30am UTC](https://www.ruby-forum.com/t/how-to-specify-access-modifers-for-variables/105685/6 "2007-06-01T11:30:34Z")

</div>

On 6/1/07, raja [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and which in fact are “methods”, thus IS IT IMPOSSIBLE TO DECLARE ACCESS  
> MODIFIERS FOR VARIABLES ?

In a word, no.

Short answer: Access modifiers only apply to methods. Instance  
variables can only be accessed through methods, and cannot be accessed  
without methods. Hence, the concept of visibility does not exist for  
instance variables in ruby.

Long answer:  
Although they are very similar concepts, please remember that there is  
a distinction between instance variables and an attributes. Although  
they can refer to the same “thing”, instance variables are the objects  
internal state, whereas attributes are (effectively) the subset of the  
instance variables which are exposed to the outside world.

If you are accustomed to languages where you can access the attributes  
of an object without methods (such as in Java), it can be a little  
confusing. In ruby, you have to use a method to access internal state.  
All instance variables are accessed via accessor methods, which  
conveniently have the same name as the variable. As a result, you  
cannot declare access modifiers for instance variables because the  
concept of visibility (private, protected, public) only exists for  
methods, and not instance variables.
