# Reflection brakes encapsulation

**URL:** <https://www.ruby-forum.com/t/reflection-brakes-encapsulation/96537>\
**Category:** Ruby\
**Created:** [June 29, 2006, 1:10pm UTC](https://www.ruby-forum.com/t/reflection-brakes-encapsulation/96537 "2006-06-29T13:10:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![keyster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@keyster](https://www.ruby-forum.com/u/keyster)\
**Post date:** [June 29, 2006, 1:10pm UTC](https://www.ruby-forum.com/t/reflection-brakes-encapsulation/96537/1 "2006-06-29T13:10:04Z")

</div>

I recently started off learning Ruby using the version 1.8.4 of the Ruby  
interpreter.  
After some time spent on trying out different Ruby’s specific features I  
now may say that it has really impressed me. It’s fun and less coding  
than other programming languages which is what I’m looking for.  
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:

class Song  
attr :count

def initialize(name, artist, duration)  
@name = name  
@artist = artist  
@duration = duration  
@count = 0  
end  
…  
def incCount  
@count += 1  
puts @count  
end  
…  
private :incCount  
end

song1 = Song.new(“Ruby Tuesday”, “ff”, 10)  
m = song1.method( :incCount )  
2.times do  
m.call  
puts “#”\*5  
end

Suppose the variable @count is part of the internal state of the object  
and it should not be changed from outside the world.

As you can see, I declared the method incCount private as it changes the  
object’s internal state  
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?

Is it a known behaviour or it has just gone unnoticed so far?

---

<div class="post-metadata">

**Author:** ![keyster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@keyster](https://www.ruby-forum.com/u/keyster)\
**Post date:** [June 29, 2006, 1:41pm UTC](https://www.ruby-forum.com/t/reflection-brakes-encapsulation/96537/2 "2006-06-29T13:41:12Z")

</div>

2006/6/29, Valentin S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![keyster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@keyster](https://www.ruby-forum.com/u/keyster)\
**Post date:** [June 29, 2006, 2:40pm UTC](https://www.ruby-forum.com/t/reflection-brakes-encapsulation/96537/3 "2006-06-29T14:40:16Z")

</div>

Ok. I’ll take it as it is and try to do nice and unharmful thinks with  
it.

Thanks

----- Original Message -----  
From: “Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Thursday, June 29, 2006 1:39 PM  
Subject: Re: Reflection brakes encapsulation

> 2006/6/29, Valentin S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > 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?

---

<div class="post-metadata">

**Author:** ![keyster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@keyster](https://www.ruby-forum.com/u/keyster)\
**Post date:** [July 1, 2006, 2:51am UTC](https://www.ruby-forum.com/t/reflection-brakes-encapsulation/96537/4 "2006-07-01T02:51:04Z")

</div>

Valentin S. wrote:

> Ok. I’ll take it as it is and try to do nice and unharmful thinks with it.

Ruby 1.9 doesn’t radically change the model, e.g.

[http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l18](http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l18)

---

<div class="post-metadata">

**Author:** ![keyster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@keyster](https://www.ruby-forum.com/u/keyster)\
**Post date:** [June 30, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/reflection-brakes-encapsulation/96537/5 "2006-06-30T19:58:04Z")

</div>

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.
