【发布时间】:2016-03-12 15:08:08
【问题描述】:
我需要定义一些设置命名空间和表类变量的类变量。
这是我正在使用的 mixin 模板:
module MyMixin
module ClassMethods
....
end
module InstanceMethods
....
end
def self.included(receiver)
namespace, table = receiver.name.underscore.pluralize.split('/')
receiver.extend ClassMethods
receiver.send :include, InstanceMethods
end
end
对于下面的代码,我希望有 namespace: 'hello' 和 table: 'worlds'
的类变量module Hello
class World
include MyMixin
end
end
对于下面的代码,我希望有 namespace: 'goodbye' 和 table: 'friends'
的类变量module Goodbye
class Friend
include MyMixin
end
end
我尝试使用receiver.class_variable_set/get,但是当我加载Goodbye::Friend代码时,Hello::World的类变量。
如何设置和分离两个类变量?
【问题讨论】:
-
Hello与hello不同。请注意大小写,对读者更友好。 -
@sawa 你是什么意思?
标签: ruby mixins class-variables