From: slowhog Date: 2010-01-26T17:33:17+09:00 Subject: Re: Is there a way to have instance_eval cannot call private/protected method? On Jan 25, 10:57 pm, slowhog 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 > 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... > > 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" > > > > }