How to specify access modifers for variables????

R

raja

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
 
S

Stefano Crocco

Alle venerd=C3=AC 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=
=20
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=20
object, you need to provide a method which does that (note that all=20
attr_reader do is to define a method which returns the instance variable).=
=20
After defining the method, you can make it protected or private, if you nee=
d=20
to.

I hope this helps

Stefano
 
V

Vin Raja

Stefano said:
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
 
L

Leonard Chin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top