我喜欢这样的想法,ActiveSupport::Concerns
但想知道是否可以在香草Ruby应用程序中使用。还是仅在Rails应用中?我正在考虑在Sinatra应用程序中使用ActiveRecord的示例。
看来您可以这样做requiring 'active_support'
,尽管我不确定是否有任何细微差别不起作用。
喜欢:
require 'active_support'
module Printable
include ActiveSupport::Concern
def print
puts "I will print here"
end
end
class User
include Printable
def initialize(name)
@name = name
end
def say_my_name
puts "my name: #{@name}"
end
end
是的,您可以(而且似乎已经发现了方法)。ActiveSupport的大部分编写方式都可以将其自身添加到其他项目中。
你甚至可以要求只 Concern
用require "active_support/concern"
我通常不赞成使用ActiveSupport::Concern
:
Concern
在所有)例子:
module
Concern
解释了相同长度和复杂性的Vanilla Ruby示例的用法Foo
模块和一个Bar
依赖于前者的模块...”,就好像您要处理mixin地狱一样本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句