2011-12-26 43 views
2

考虑下面的代码:红宝石代表团/代理

class ArrayProxy < BasicObject 
    def initialize 
    @array = [] 
    end 

    def foo 
    puts 'foo' 
    end 

    def method_missing(*args, &block) 
    @array = @array.send(*args, &block) 
    end 

    def self.method_missing(*args, &block) 
    new.send(*args, &block) 
    end 
end 

为什么调用“富”被委托给数组?

ruby-1.9.2-p290 :018 > ArrayProxy.new << 1 
=> [1] 
ruby-1.9.2-p290 :019 > ArrayProxy << 1 
=> [1] 
ruby-1.9.2-p290 :020 > ArrayProxy.new.foo 
foo 
=> nil 
ruby-1.9.2-p290 :021 > ArrayProxy.foo 
NoMethodError: undefined method `foo' for []:Array 
+0

在代码中尝试'__send__'。这可能会做到。 – Linuxios 2011-12-26 19:01:52

+0

这样做,从文档'您可以使用__send__如果名称发送冲突与obj中的现有方法'。但'foo'不是Object中的方法吗?有人可以澄清接受的答案吗? – Chris 2011-12-26 19:06:03

+0

顺便说一句,为什么你重新分配'@ array'内'method_missing'? – 2011-12-26 19:22:29

回答

6

由于Linux_iOS.rb.cpp.c.lisp.m.sh在注释中提到,你应该在这种情况下使用__send__方法,如BasicObject没有定义实例方法send

Object.instance_methods.grep /send/ 
# => [:send, :public_send, :__send__] 

BasicObject.instance_methods.grep /send/ 
# => [:__send__] 

,可以也可以通过BasicObject的文档证明。

缺席在BasicObect类结果send实例方法的呼叫中的下列链:

# initial call 
ArrayProxy.foo 

# there's no class method 'foo', so we go to class 'method_missing' method 
ArrayProxy.method_missing :foo 

# inside class 'method_missing' we delegate call to new instance using 'send' 
ArrayProxy.new.send :foo 

# there is no instance method 'send' in ArrayProxy class (and its parent class 
# BasicObject) so instance 'method_missing' is called 
ArrayProxy.new.method_missing :send, :foo 

# instance 'method_missing' delegates call of 'send' method to @array 
@array.send :send, :foo 

# that is unfolded into regular call of 'send' on @array object 
@array.send :foo 

# and finally 'foo' is called for @array object 
@array.foo 
# => NoMethodError: undefined method `foo' for []:Array 
+0

伟大的穿越,谢谢=] – Chris 2011-12-26 22:22:37

0

的签名method_missingmethod_sym, *args, &block

我认为这是发送到阵列,因为您正在呼叫new在类级method_missing声明(它实例化一个新的ArrayProxy)并调用发送返回值。

我对于为什么要设置@array等于返回值@array.send(*args, &block)在实例级别声明method_missing中存在一些困惑。

编辑:这是相当古怪的行为。我们希望发送:fooArrayProxy的实例来打印foo,而不是将呼叫委托给其@arraymethod_missing

1

也许它将使使用Ruby的标准库的工具,而不是滚动您自己更有意义?

Delegator class。 (我指的是1.9.3文档,但该类也存在于1.8.x中)。

+0

不一定。如果你使用'Delegator'或者它的子类,'class'等方法将不会被委派。 'BasicObject'最小的一组方法可以让你创建一个对象,在'Delegator'允许的情况下,可以在更多场景下对原始对象进行“替身”。 – Kelvin 2014-04-24 19:42:08