|
|
Is there a way to have instance_eval cannot call private/protected method?
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
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"
}
|
|
0
|
|
|
|
Reply
|
slowhog
|
1/26/2010 6:57:01 AM |
|
On Jan 25, 10:57=A0pm, slowhog <henry...@ztune.net> 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
> =A0 private
> =A0 =A0 def method1(param)
> =A0 =A0 =A0 puts "Test Method 1: #{param}"
> =A0 =A0 end
> =A0 public
> =A0 =A0 def method2(param)
> =A0 =A0 =A0 puts "Test Method 2: #{param}"
> =A0 =A0 end
> end
>
> def real_object(&block)
> =A0 x =3D RealObject.new
> =A0 x.instance_eval &block
> end
>
> real_object {
> =A0 method2 "m2"
> =A0 method2 "m2.2"
> =A0 method1 "please fail me"
>
>
>
> }
|
|
0
|
|
|
|
Reply
|
slowhog
|
1/26/2010 8:33:17 AM
|
|
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
|
|
0
|
|
|
|
Reply
|
burke
|
1/27/2010 1:50:46 AM
|
|
|
2 Replies
150 Views
(page loaded in 0.036 seconds)
Similiar Articles: Changing access specifiers during inheritance - comp.lang.c++ ...... destructor as private virtual, will make the call ... (Since you cannot derive from the class, there's no ... class, it will have either a public virtual destructor, or a protected ... Displaying Images - comp.lang.java.guiIf you cannot get that ... For part 2), there are a number of ways you ... 0 so you do not have to create an instance of IconLoader. Just call * the static method. [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ...Archive-name: cdrom/cd-recordable/part1 Posting-Frequency: monthly Last-modified: 2008/10/09 Version: 2.71 Send corrections and updates to And... Access Modifiers (C# Programming Guide) - Microsoft Corporation ...For example, you cannot have a public method M that ... Likewise, you cannot have a protected property of ... of seen an externally private, internally protected. Method (computer programming) - Wikipedia, the free encyclopediathis cannot be used in static methods. In ... can be computed at runtime, there being no difference between calling a method on a ... following C++, class geometry have two method ... 7/24/2012 11:27:52 AM
|
|
|
|
|
|
|
|
|