【问题标题】:Is there a way to extends a base module when I use thor?当我使用 thor 时,有没有办法扩展基本模块?
【发布时间】:2015-06-26 07:37:10
【问题描述】:

我想在一个rails项目下做一个batch。

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

# 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)

我该怎么做?

【问题讨论】:

    标签: ruby-on-rails thor


    【解决方案1】:

    如果我正确理解您的担忧,您应该将您的 BatchBase 声明为一个模块,您将拥有类似的东西

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

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

    Ruby 没有多重继承,你应该使用 mixins 来代替。 你可以阅读更多关于它here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-10
      • 2011-02-03
      • 2018-02-27
      • 2021-08-25
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多