可能我遗漏了一些简单的东西,但我不明白如何使用Ruby的DelegateClass方法,我的意思是什么时候使用它而不是SimpleDelegator类(class)。例如,以下所有内容的工作原理几乎相同:require'delegate'a=SimpleDelegator.new([0])b=DelegateClass(Array).new([0])c=DelegateClass(String).new([0])a[0,1]pb#=>[0,2]pc#=>[0,3]请注意,将哪个类传递给DelegateClass似乎并不重要。 最佳答案
我已将Rails3.2应用程序从Ruby1.9.3-p448升级到2.0.0-p451。所有自动化测试都通过了,除了一个,但有错误:NameError:undefinedlocalvariableormethod'subject_path'for#...'这里的代码有点复杂,但本质上是subject_path提供方法是因为EntityAssociationsResponder继承自SimpleDelegator,并使用当前的RailsController进行初始化,该Controller实现了subject_path作为一种protected方法。该方法受到保护,因此它不会被Rails
我的代码使用SimpleDelegatorrequire'delegate'classFoo"new_meth"foo.bar_meth#=>"old_meth"为什么最后一行给出"old_meth"???!!!谢谢! 最佳答案 Delegator说:Thislibraryprovidesthreedifferentwaystodelegatemethodcallstoanobject.TheeasiesttouseisSimpleDelegator.Passanobjecttotheconstructorandallmethods