繁体   English   中英

使用thor时,是否可以扩展基本模块?

[英]Is there a way to extends a base module when I use thor?

我想在Rails项目下进行批量生产。

我创建了一个基本批处理模块:

# lib/tasks/batch_base.rb
module Tasks
  class BatchBase
    def run
      # Run something
    end
  end
end

现在我想和托尔做一批。

# lib/tasks/another_batch.rb
require 'thor'
module Tasks
  class AnotherBatch < Thor
    desc 'test', 'sample'
    def hello(name)
      # Do something
    end
  end
end

在这里,我想扩展基本批处理,但是当我尝试时:

class AnotherBatch < BatchBase < Thor
# or
class AnotherBatch < Thor < BatchBase

没用 错误:

superclass must be a Class (NilClass given) (TypeError)

我该怎么做?

如果我正确理解了您的担忧,则应将BatchBase声明为模块,您将获得类似的信息

 
module Tasks
  module BatchBase
    def run
    ...
    end
  end
end 

require 'thor' module Tasks class AnotherBatch < Thor include BatchBase end end

Ruby没有多重继承,您应该改用mixins。 您可以在这里了解更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM