【发布时间】:2017-11-10 12:43:37
【问题描述】:
我必须以优雅和孤立的方式将一些行为包裹在外部 gem 周围。鉴于下面的抽象,一切都运行顺利,但从来没有打印'bar'。 谁能告诉我为什么?
我的代码:
module RefineGem
refine GemMainModule::GemClass do
def self.foo
p 'bar'
super
end
end
end
module Test
using RefineGem
def test
GemMainModule::GemClass.foo
end
end
class Testing
include Test
end
Testing.new.test
宝石代码:
module GemMainModule
class Base
include GemMainModule::Fooable
end
class GemClass < Base
end
end
module GemMainModule
module Fooable
extend ActiveSupport::Concern
class_methods do
def foo
p 'zoo'
end
end
end
end
【问题讨论】:
-
我从来没有使用过
refinements,因为前置模块是一种更干净、健壮和智能的方法;但我建议你refine GemMainModule::GemClass.singleton_class { def foo ... end }。我怀疑改进是否适用于类方法。 -
无论如何,然后使用模块作为答案发布您的解决方案,我会接受它。
-
“使用模型”是什么意思?
-
这是一个错字,我的意思是模块
标签: ruby ruby-on-rails-5 refinements