Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
元编程Ruby:使用特定的超类构造函数_Ruby_Inheritance_Constructor_Metaprogramming - Fatal编程技术网

元编程Ruby:使用特定的超类构造函数

元编程Ruby:使用特定的超类构造函数,ruby,inheritance,constructor,metaprogramming,Ruby,Inheritance,Constructor,Metaprogramming,在Ruby中,我希望在子类中使用超类的特定构造函数。我想这样做是因为我已经动态地操纵了超类构造函数。相应的子类也是动态创建的 我已经找到了一种方法,但是由于我对Ruby元编程不是很熟悉,我想知道是否有更好的方法 下面是我的类和我当前的解决方案。感谢您的阅读和思考: class A < Object def self.new *args super #doing things end end class B < A def self.new *args

在Ruby中,我希望在子类中使用超类的特定构造函数。我想这样做是因为我已经动态地操纵了超类构造函数。相应的子类也是动态创建的

我已经找到了一种方法,但是由于我对Ruby元编程不是很熟悉,我想知道是否有更好的方法

下面是我的类和我当前的解决方案。感谢您的阅读和思考:

class A < Object
  def self.new *args
    super
    #doing things
  end
end

class B < A
  def self.new *args
    #doing things
    return superclass.superclass.method(:new).unbind.bind(self).call(args)
  end
end

这段代码给了我一个ArgumentError:1.9下的0错误的arguments1错误数。。。我在1.9.2上运行良好,我可以在irb和脚本中同时运行A.new和B.new。您确定运行的代码与此问题中的代码完全相同吗?通过使用两个编辑器复制问题中的代码进行双重检查,这两个编辑器都可以运行。你知道是什么原因导致的吗?至少,我可以证明它在1.8.6中不起作用: