我想要一个主类,并在其中包含一些我想在其他类中调用的变量。例如:
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。这样做的最佳方式是什么?
答案 0 :(得分:0)
最好的方法是从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
end