[英]Is there a way to extends a base module when I use thor?
I want to make a batch under a rails project. 我想在Rails项目下进行批量生产。
I have created a base batch module: 我创建了一个基本批处理模块:
# lib/tasks/batch_base.rb
module Tasks
class BatchBase
def run
# Run something
end
end
end
Now I want to make a batch with thor . 现在我想和托尔做一批。
# lib/tasks/another_batch.rb
require 'thor'
module Tasks
class AnotherBatch < Thor
desc 'test', 'sample'
def hello(name)
# Do something
end
end
end
Here I want to extends the base batch, but when I try: 在这里,我想扩展基本批处理,但是当我尝试时:
class AnotherBatch < BatchBase < Thor
# or
class AnotherBatch < Thor < BatchBase
It doesn't work. 没用 Error: 错误:
superclass must be a Class (NilClass given) (TypeError)
How can I do it? 我该怎么做?
If I understand your concerns correctly you should declare your BatchBase
as a module and you'll have something like that 如果我正确理解了您的担忧,则应将BatchBase
声明为模块,您将获得类似的信息
module Tasks
module BatchBase
def run
...
end
end
end
require 'thor' module Tasks class AnotherBatch < Thor include BatchBase end end
Ruby does not have multiple inheritance, you should use mixins instead. Ruby没有多重继承,您应该改用mixins。 You can read more about it here 您可以在这里了解更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.