[英]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.