我正在处理需要不同类的项目,如下所示:
LIB / command.rb
class Command
end
LIB /命令/ group.rb
class Command
class Group < Command
end
end
LIB /命令/组/ add.rb
class Command
class Group
class Add < Group
# do something
end
end
end
和lib / group.rb
class Group
# do something
end
Rake文件
task :reload do
Dir[File.dirname(__FILE__) + '/lib/**/*.rb'].each{ |file| load file }
end
task default: 'reload'
前三个类的行为类似于助手,最后一个类是模型。
当我运行Rakefile
来加载所有类时,它确实会提升TypeError: superclass mismatch for class Group
。
如何在不重命名其中一个Group
类的情况下解决它?可能吗?
lib/command/group/add.rb
可能会在lib/command/group.rb
之前装满。因此在后者中,似乎你试图改变Group
继承的类。
乐队援助解决方案将指向所有文件中的相同子类。在lib/command/group/add.rb
的阿卡,你应该添加< Command
。
真正的解决方案应该是永远不要使用类/模块进行命名空间并为其附加功能。
这个问题在Euruko 2016中提出,Matz说他们可能会考虑一个特殊的关键字。 [需要引证]
感谢您提供更多代码。现在很清楚,这里有一个错误。你定义Command::Group
两次,但只有一个继承自Command
LIB /命令/ group.rb
class Command
class Group < Command
end
end
LIB /命令/组/ add.rb
class Command
class Group # missing inheritance
class Add < Group
# do something
end
end
end