From: David Pollak Date: 2006-07-01T02:57:15+09:00 Subject: Re: Reflection brakes encapsulation ------=_Part_6153_7409905.1151684337109 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline The same reflection issues are present in Java and C#. I know of no language and/or runtime that completely enforces protection of private methods. On 6/29/06, Valentin Samareanu wrote: > > Ok. I'll take it as it is and try to do nice and unharmful thinks with it. > > Thanks > > ----- Original Message ----- > From: "Robert Klemme" > To: "ruby-talk ML" > Sent: Thursday, June 29, 2006 1:39 PM > 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/ > > > > > -- -------- David Pollak's Ruby Playground http://dppruby.com ------=_Part_6153_7409905.1151684337109--