当我使用 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

现在我想用 thor 做一个批次。

# 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 </p>

<p>require 'thor'
module Tasks
  class AnotherBatch < Thor
    include BatchBase
  end
end
</pre>

Ruby 没有多重继承,你应该使用 mixins 代替。 您可以阅读更多相关信息 here