I'm thinking in:
class X
def new()
@a = 1
end
def m( other )
@a == other.@a
end
end
x = X.new()
y = X.new()
x.m( y )
But it doesn't works.
The error message is:
syntax error, unexpected tIVAR
How can I compare two private attributes from the same class then?
There are several methods
Getter:
instance_eval
:instance_variable_get
:I don't think ruby has a concept of "friend" or "protected" access, and even "private" is easily hacked around. Using a getter creates a read-only property, and instance_eval means you have to know the name of the instance variable, so the connotation is similar.
Not sure, but this might help:
Outside of the class, it's a little bit harder:
http://whynotwiki.com/Ruby_/_Variables_and_constants#Variable_scope.2Faccessibility
If you don't use the
instance_eval
option (as @jleedev posted), and choose to use agetter
method, you can still keep itprotected
If you want a
protected
method in Ruby, just do the following to create a getter that can only be read from objects of the same class:There have already been several good answers to your immediate problem, but I have noticed some other pieces of your code that warrant a comment. (Most of them trivial, though.)
Here's four trivial ones, all of them related to coding style:
Anyway, that's just the small stuff. The big stuff is this:
This does not do what you think it does! This defines an instance method called
X#new
and not a class method calledX.new
!What you are calling here:
is a class method called
new
, which you have inherited from theClass
class. So, you never call your new method, which means@a = 1
never gets executed, which means@a
is always undefined, which means it will always evaluate tonil
which means the@a
ofself
and the@a
ofother
will always be the same which meansm
will always betrue
!What you probably want to do is provide a constructor, except Ruby doesn't have constructors. Ruby only uses factory methods.
The method you really wanted to override is the instance method
initialize
. Now you are probably asking yourself: "why do I have to override an instance method calledinitialize
when I'm actually calling a class method callednew
?"Well, object construction in Ruby works like this: object construction is split into two phases, allocation and initialization. Allocation is done by a public class method called
allocate
, which is defined as an instance method of classClass
and is generally never overriden. It just allocates the memory space for the object and sets up a few pointers, however, the object is not really usable at this point.That's where the initializer comes in: it is an instance method called
initialize
, which sets up the object's internal state and brings it into a consistent, fully defined state which can be used by other objects.So, in order to fully create a new object, what you need to do is this:
[Note: Objective-C programmers may recognize this.]
However, because it is too easy to forget to call
initialize
and as a general rule an object should be fully valid after construction, there is a convenience factory method calledClass#new
, which does all that work for you and looks something like this:[Note: actually,
initialize
is private, so reflection has to be used to circumvent the access restrictions like this:obj.send(:initialize, *args, &block)
]Lastly, let me explain what's going wrong in your
m
method. (The others have already explained how to solve it.)In Ruby, there is no way (note: in Ruby, "there is no way" actually translates to "there is always a way involving reflection") to access an instance variable from outside the instance. That's why it's called an instance variable after all, because it belongs to the instance. This is a legacy from Smalltalk: in Smalltalk there are no visibility restrictions, all methods are public. Thus, instance variables are the only way to do encapsulation in Smalltalk, and, after all, encapsulation is one of the pillars of OO. In Ruby, there are visibility restrictions (as we have seen above, for example), so it is not strictly necessary to hide instance variables for that reason. There is another reason, however: the Uniform Access Principle.
The UAP states that how to use a feature should be independent from how the feature is implemented. So, accessing a feature should always be the same, i.e. uniform. The reason for this is that the author of the feature is free to change how the feature works internally, without breaking the users of the feature. In other words, it's basic modularity.
This means for example that getting the size of a collection should always be the same, regardless of whether the size is stored in a variable, computed dynamically every time, lazily computed the first time and then stored in a variable, memoized or whatever. Sounds obvious, but e.g. Java gets this wrong:
vs.
Ruby takes the easy way out. In Ruby, there is only one way to use a feature: sending a message. Since there is only one way, access is trivially uniform.
So, to make a long story short: you simply can't access another instance's instance variable. you can only interact with that instance via message sending. Which means that the other object has to either provide you with a method (in this case at least of
protected
visibility) to access its instance variable, or you have to violate that object's encapsulation (and thus lose Uniform Access, increase coupling and risk future breakage) by using reflection (in this caseinstance_variable_get
).Here it is, in all its glory:
Or alternatively:
Which one of those two you chose is a matter of personly taste, I would say. The
Set
class in the standard library uses the reflection version, although it usesinstance_eval
instead:(I have no idea why. Maybe
instance_variable_get
simply didn't exist whenSet
was written. Ruby is going to be 17 years old in February, some of the stuff in the stdlib is from the very early days.)