From: Robert Klemme Date: 2006-06-29T20:39:34+09:00 Subject: Re: Reflection brakes encapsulation 2006/6/29, Valentin Samareanu : > > However, there is something related to OO concepts that I think, in Ruby, is looking for troubles. More exactly, the encapsulation can be broken using reflection. Here is a piece of cod that illustrates this: > As you can see, I declared the method incCount private as it changes the object's internal state You can even go further and directly modify instance variables with instance_variable_set and instance_variable_set. > But, surprise, the method is not quite genuine private. It can be called outside the class using reflection. > My question is if it's OK to let code outside the class to change the internal state of an instance of that class? I mean why should we bother making methods private in the first place if they can be called outside the class anyway? Ruby prevents normal usage of private methods but still let's you use them if you know what you do. So the answer is, yes, bother to make things private that should stay private. Anybody messing with the innards of your classes will be immediately punished if he does it in non conforming ways - but he *can* do it if needed. That's Ruby's pragmatic approach. > Is it a known behaviour or it has just gone unnoticed so far? Umm, Ruby has been around for some while now (meaning years) - such a fundamental design cannot go unnoticed for such a long time. Kind regards robert -- Have a look: http://www.flickr.com/photos/fussel-foto/