English 中文(简体)
Fooling Duncan 案操作员=与代理对象
原标题:Fooling Ruby s case operator, ===, with proxy objects

我试图制造一个代理人,把几乎所有方法都传给儿童物体,主要是代议制。 大部分情况下,我只是使用一种基本目标,用一种方法——接收儿童物体。 迄今为止,情况良好。

骗局是我可能尝试的,因此,我可以 t笑鲁·鲁比斯案件操作员,因此我可以做:

x = Proxy.new(15)
Fixnum === x #=> false, no matter what I do

这当然会使任何“<条码>---条码>业务失败,这意味着轴线可以安全地移交给其他图书馆。

我的寿命可以指什么是用的。 代理人对所有基于阶级的“一”内乱预案进行罚款。

x.is_a?(Fixnum) #=> true
x.instance_of?(Fixnum) #=> true
x.kind_of?(Fixnum) #=> true
x.class #=> Fixnum

Is Module#= 只是做某种可以避免的魔术?

最佳回答

是Yah。 <代码>Module#=在C中实施,直接审查物体类别等级。 它不像现在这样看待它。

问题回答

问题是,它的确是Fixnum ==x,这意味着=上,而不是在上。 您可以取代现有的所有<条码>=方法(并观察采用新的<条码>=方法时),但这将是许多工作,非常脆弱。

I think what you re looking for is the Delegator class.

Your Proxy class should subclass the Delegator class, then define __getobj__ and __setobj__ to get and set the target object.

就这一点而言,我尝试了自己,而且没有工作。

EDIT:

正如dev夫所说,技术问题是,Fixnum正在被送上:===方法。 然而,我认为,进一步思考这个问题是正确的。 由于一名代表应是一个抽象的接口,以掩盖执行细节,因此Peoxy的事例没有被正确地确定为一种。 Fixnum.

如果你真的想要Proxy的等级是菲克斯南的一类,但希望与方法脱钩,那么合乎逻辑的做法是,要么是子级Fixnum,要么是创建一种ProxyMethods模块,并延长Fixnum的个别情况。

当然,由于你能够真正地做Fixnum.new,你不得不将Fixnum下级,以便延长一次,但一般规则仍然有效。

you should probably do a search for the BlankSlate class. This class strips out most of the methods from a normal Object and the website has an example of a simple Proxy class that will print out all methods being called. This shoul give you a better picture of what is happening. Sorry I can t give you a fuller answer but I m on my phone. Hope that helps.





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签