我希望有一个主类,在它里面有一些变量,我想在其他类中调用这些变量。例如:
class AwsS3Initalization
attr_reader :resource, :client
def initialize resource, client
@resource = resource
@client = client
# Where do I define those variables below? I think this is not the right place to put them
@resource = Aws::S3::Resource.new(region: 'region', access_key_id: 'my-key', secret_access_key: 'secret-key')
@client = Aws::S3::Client.new(region: 'region', access_key_id: 'my-key', secret_access_key: 'secret-key')
end
end我希望能够在我拥有的其他类中调用@resource和@client。做这件事最好的方法是什么?
发布于 2016-08-01 11:55:34
最好的方法是从AwsS3Initalization继承其他类,例如:
class Foo < AwsS3Initalization
end
class Bar < AwsS3Initalization
end然后,您将能够访问从父类到子类的所有变量。
更新
class AwsS3Initialization
ACCESS_KEY = <read-from-yml>
SECRET_KEY = <read-from-yml>
def self.resource
Aws::S3::Resource.new(...)
end
def self.client
Aws::S3::Client.new(...)
end
endhttps://stackoverflow.com/questions/38698147
复制相似问题