我如何使用层次结构修补宝石?

时间:2015-06-12 16:42:10

标签: ruby gem thor

我正在尝试为Thor宝石添加新方法。具体来说,我想为Thor :: Shell :: Basic添加一个方法。在基于thor的gem中,在bin / mycommand中,我有这个:

require 'thor'
require 'ext/thor/extension'
Thor.include ThorExtensions::Thor::Shell::Basic
MyCommand.start

在lib / ext / thor / extension.rb中,我有:

module ThorExtensions
  module Thor
    module Shell
      module Basic
        def extension_method

当我调用extension_method时,我收到一条错误消息,表示它无法读取Thor :: Shell :: Basic的属性(具体为padding)。当我进入Thor :: Shell :: Basic时,在pry中,我看到我的方法被列为该类的方法,但它似乎无法在运行时访问它。我做错了吗?

1 个答案:

答案 0 :(得分:1)

您可以在ruby中重新打开课程:

hash.getrandbits(<number of bits>)

或者你可以将你的扩展作为一个模块(我更喜欢这个):

class Thor::Shell::Basic
  def extension_method
  end
end
相关问题