# Is there a way to have instance\_eval cannot call private/protected method?

**URL:** <https://www.ruby-forum.com/t/is-there-a-way-to-have-instance-eval-cannot-call-private-protected-method/182296>\
**Category:** Ruby\
**Created:** [January 26, 2010, 8:01am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-have-instance-eval-cannot-call-private-protected-method/182296 "2010-01-26T08:01:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![slowhog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@slowhog](https://www.ruby-forum.com/u/slowhog)\
**Post date:** [January 26, 2010, 8:01am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-have-instance-eval-cannot-call-private-protected-method/182296/1 "2010-01-26T08:01:09Z")

</div>

Hi,

Is there any mechanism to do what instance\_eval but honor the  
visibility? I want to something like following, but would like the  
block passed to real\_object can only only have access to public  
methods.

I did a little search, seems like 1.8.5 does that, but was changed in  
1.8.6[1]. While I don’t know the history, I am interested to learn  
about the rational, and if there is a replacement for that behavior.

Thanks in advance,  
Henry

[1]  
[http://jamesgolick.com/2007/9/28/ruby-1-8-5-\_eval-methods-are-subject-to-access-modifiers-on-self.html](http://jamesgolick.com/2007/9/28/ruby-1-8-5-_eval-methods-are-subject-to-access-modifiers-on-self.html)

class RealObject  
private  
def method1(param)  
puts “Test Method 1: #{param}”  
end  
public  
def method2(param)  
puts “Test Method 2: #{param}”  
end  
end

def real\_object(&block)  
x = RealObject.new  
x.instance\_eval &block  
end

real\_object {  
method2 “m2”  
method2 “m2.2”  
method1 “please fail me”  
}

---

<div class="post-metadata">

**Author:** ![slowhog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@slowhog](https://www.ruby-forum.com/u/slowhog)\
**Post date:** [January 26, 2010, 9:33am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-have-instance-eval-cannot-call-private-protected-method/182296/2 "2010-01-26T09:33:38Z")

</div>

On Jan 25, 10:57 pm, slowhog [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> Is there any mechanism to do what instance\_eval but honor the  
> visibility?

I probably should clarify that I would like to know if there is a  
‘right’ way to do it, other than hacking something like another layer  
to be the ‘API’ object which hide the real object.

Cheers,  
Henry

---

<div class="post-metadata">

**Author:** ![slowhog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bb73d2/32.png) [@slowhog](https://www.ruby-forum.com/u/slowhog)\
**Post date:** [January 27, 2010, 2:55am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-have-instance-eval-cannot-call-private-protected-method/182296/3 "2010-01-27T02:55:53Z")

</div>

I don’t think there’s any way to do quite exactly this, since calling,  
for example, ‘method2 “m2”’ relies on self being set to an instance of  
RealObject. If self is an instance of RealObject, there’s no (sane)  
way to hide private methods.

You might want to consider something like:

def real\_object(&block)  
x = RealObject.new  
yield x  
end

real\_object do |o|  
o.method2 “m2”  
o.method2 “m2.2”  
o.method1 “please fail me”  
end

Burke
