Ruby 在许多类中使用相同的一组属性
请帮帮我 我需要在许多类中使用相同的一组属性。我建议创建具有预定义属性的模块,并在每个类中扩展该模块Ruby 在许多类中使用相同的一组属性,ruby,Ruby,请帮帮我 我需要在许多类中使用相同的一组属性。我建议创建具有预定义属性的模块,并在每个类中扩展该模块 module Basic @a=10 end class Use extend Basic def self.sh @a end end puts Use.sh 但是输出是空的。我好像错过了什么 也许有更好的办法 你的想法?是: 有selfBasic评估功能。扩展后的代码时,您希望它计算为使用: module Basic # self = Basic, but method
module Basic
@a=10
end
class Use
extend Basic
def self.sh
@a
end
end
puts Use.sh
但是输出是空的。我好像错过了什么
也许有更好的办法
你的想法?是:
有self
Basic评估功能。扩展后的代码时,您希望它计算为使用
:
module Basic
# self = Basic, but methods defined for instances
class << self
# self = Basic's eigenclass
def extended(base)
base.class_eval do
# self = base due to class_eval
@a=10
end
end
end
end
class Use
# self = Use, but methods defined for instances
extend Basic # base = Use in the above
class << self
# self = Use's eigenclass
def sh
@a
end
end
end
Use.sh # 10
基本模块
#self=基本,但为实例定义的方法
类它是:
有self
Basic评估功能。扩展后的代码时,您希望它计算为使用
:
module Basic
# self = Basic, but methods defined for instances
class << self
# self = Basic's eigenclass
def extended(base)
base.class_eval do
# self = base due to class_eval
@a=10
end
end
end
end
class Use
# self = Use, but methods defined for instances
extend Basic # base = Use in the above
class << self
# self = Use's eigenclass
def sh
@a
end
end
end
Use.sh # 10
基本模块
#self=基本,但为实例定义的方法
类您所描述的是Flyweight设计模式。虽然有些人认为这在ruby()中很少使用,但其他人提供了一个实现(第14页)
就我个人而言,我要做的是将所有这些属性放入yaml文件中,并将它们解析为全局变量:
ATTRIBUTES=YAML.load\u文件(file.expand\u路径('ATTRIBUTES.yml',file.dirname(文件))
或者一个类方法(这里有缓存,假设你不会在应用程序运行时更改yml文件,并且需要新的值)。我建议在这里使用ActiveSupport::Concern
,因为它比传统的类内混合方法更容易阅读:
module Basic
extend ActiveSupport::Concern
module ClassMethods
def attributes_file
File.expand_path('attributes.yml', File.dirname(__FILE__))
def attributes
@attributes ||= YAML.load_file(attributes_file)
@attributes
end
end
module InstanceMethods
# define any here that you need, like:
def attributes
self.class.attributes
end
end
end
您可以为每个属性定义方法,或者依赖于在属性哈希中建立索引。您还可以使用fancy和define method_missing来检查是否存在具有该名称的属性,这样您就不必继续添加方法,因为您希望向共享配置添加更多属性。您所描述的是Flyweight设计模式。虽然有些人认为这在ruby()中很少使用,但其他人提供了一个实现(第14页)
就我个人而言,我要做的是将所有这些属性放入yaml文件中,并将它们解析为全局变量:
ATTRIBUTES=YAML.load\u文件(file.expand\u路径('ATTRIBUTES.yml',file.dirname(文件))
或者一个类方法(这里有缓存,假设你不会在应用程序运行时更改yml文件,并且需要新的值)。我建议在这里使用ActiveSupport::Concern
,因为它比传统的类内混合方法更容易阅读:
module Basic
extend ActiveSupport::Concern
module ClassMethods
def attributes_file
File.expand_path('attributes.yml', File.dirname(__FILE__))
def attributes
@attributes ||= YAML.load_file(attributes_file)
@attributes
end
end
module InstanceMethods
# define any here that you need, like:
def attributes
self.class.attributes
end
end
end
您可以为每个属性定义方法,或者依赖于在属性哈希中建立索引。您还可以选择并定义method_missing,以检查是否存在具有该名称的属性,这样您就不必在希望向共享配置添加更多属性时不断添加方法。如果希望更改值,以及在类之间共享更新的值,那么单例可能是最好的方法。如果您希望更改值,并在类之间共享更新的值,那么单例可能是最好的方法。感谢您的帮助,再问一个问题,我不知道什么是extended(base)
的实际意思是。能给我一个链接吗?谢谢!有点像:这是一个模块方法,当你使用扩展Basic
(它然后调用Basic.extend(use)
)。include()
在你使用包含Basic
时生效。谢谢你的帮助,还有一个问题,我想不出什么是扩展的(base)
实际上的意思是。可以给我一个链接吗?谢谢!有点像:这是一个模块方法,当你使用扩展Basic
(然后它调用基本。扩展(使用)
)。包含()
在你使用包含Basic
时生效。