I have an aws_helper.rb file
require 'yaml'
require 'fog'
class Aws_Helper
def initialize()
conf = YAML::load_file("config.yml")
@connection = Fog::DNS.new( :provider=> 'aws',
:aws_access_key_id => conf['aws_access_key'],
:aws_secret_access_key => conf['aws_secret_key']
)
return @connetion
end
end
If I use the class from another file, say test.rb
require_relative 'aws_helper.rb'
connection = Aws_Helper.new()
connection.zones.get("ZXASDFS443")
p connection
I get the error,
undefined method `zones' for # (NoMethodError)
But from aws_helper.rb file itself if I do @connection.zone.get("ZXASDFS443") this works fine.
What I am doing wrong here?
I think the problem comes from how initialize
works in Ruby, which is a little weird. In particular, unlike most other methods, initialize
ignores the return value provided. It will ALWAYS return an instance of the class it is defined upon. So in this case you would get back an Aws_Helper instance (instead of a reference to the connection itself). If you change the name of the method there to something like connect
it should work the way you have laid out.