Ruby中类的超类不匹配

Ruby中类的超类不匹配,ruby,Ruby,我正在从事的项目要求不同的类具有相同的名称,如下所示: lib/command.rb class Command end lib/command/group.rb class Command class Group < Command end end class Command class Group < Command end end 耙子 task :reload do Dir[File.dirname(__FILE__) + '/lib/**/*.rb

我正在从事的项目要求不同的类具有相同的名称,如下所示:

lib/command.rb

class Command
end
lib/command/group.rb

class Command
  class Group < Command
  end
end
class Command
  class Group < Command
  end
end
耙子

task :reload do
  Dir[File.dirname(__FILE__) + '/lib/**/*.rb'].each{ |file| load file }
end

task default: 'reload'
前三个类的行为类似于助手,最后一个类是模型

当我运行Rakefile加载所有类时,它将引发TypeError:类组的超类不匹配


在不重命名其中一个组类的情况下,如何解决该问题?可能吗?

感谢您提供更多代码。现在很清楚,这里有一个错误。您定义了两次Command::Group,但其中只有一个继承自Command

lib/command/group.rb

class Command
  class Group < Command
  end
end
class Command
  class Group < Command
  end
end
lib/command/group/add.rb

class Command
  class Group
    class Add < Group
      # do something
    end
  end
end
class Command
  class Group # missing inheritance
    class Add < Group
      # do something
    end
  end
end

感谢您提供进一步的代码。现在很清楚,这里有一个错误。您定义了两次Command::Group,但其中只有一个继承自Command

lib/command/group.rb

class Command
  class Group < Command
  end
end
class Command
  class Group < Command
  end
end
lib/command/group/add.rb

class Command
  class Group
    class Add < Group
      # do something
    end
  end
end
class Command
  class Group # missing inheritance
    class Add < Group
      # do something
    end
  end
end
lib/command/group/add.rb可能在lib/command/group.rb之前加载。因此,在后者中,似乎您试图更改从哪个类组继承

创可贴的解决方案是指向所有文件中的相同子类。在lib/command/group/add.rb中,您应该添加 真正的解决方案应该是永远不要将类/模块用于名称空间,并将功能附加到它

这个问题是在Euruo 2016中提出的,Matz说他们可能会考虑一个特殊的关键字。[需要引用]

lib/command/group/add.rb可能在lib/command/group.rb之前加载。因此,在后者中,似乎您试图更改从哪个类组继承

创可贴的解决方案是指向所有文件中的相同子类。在lib/command/group/add.rb中,您应该添加 真正的解决方案应该是永远不要将类/模块用于名称空间,并将功能附加到它


这个问题是在Euruo 2016中提出的,Matz说他们可能会考虑一个特殊的关键字。[需要引用]

不可能;以上是完美的加载。问题应该在别的地方。您可以尝试将上面的内容复制粘贴到一个单独的文件中,并尝试将其复制到ruby file.rb。这意味着您打开了同一个组类两次,但第二个类使用了不同的超类