0

I wonder if there's a solution to this issue, designing modules:

Amazon has a module with top-level name space AWS. It contains AWS::EC2 among others.

I am writing a module that provides more AWS functionality. I wanted to call it MY_ORG_NAME::AWS. It would have methods such as MY_ORG_NAME::AWS.instance_id.

I've hit a stumbling block, however:

require 'aws-sdk'

module MY_ORG_NAME
  module AWS
    def self.instance_id
      ec2 = AWS::EC2.new  # Error
      ...
    end
  end
end

The interpreter returns an error for that line above: *uninitialized constant MY_ORG_NAME::AWS::EC2*. I see what's going on. But I'm not sure there's anything I can do about it. It looks like the problem is that Amazon did not put their AWS module into a module name space such as AMAZON.

Is there some simple way to solve this, or some mistake I've made? My current best idea is to call my AWS module something other than AWS.

2 Answers 2

3

Isn't it just?

module MY_ORG_NAME
  module AWS
    def self.instance_id
      ec2 = ::AWS::EC2.new # <-- :: goes to the top-level namespace
      ...
    end
  end
end
Sign up to request clarification or add additional context in comments.

Comments

1

Well, you could "alias" the Amazon one. Modify your code like this:

require 'aws-sdk'

Amazon = AWS

module MY_ORG_NAME
  module AWS
    def self.instance_id
      ec2 = Amazon::EC2.new
      ...
    end
  end
end

2 Comments

Pretty sure you're over-thinking this ;) Ruby already provides the ability to reference the top-level namespace.
I knew about the top-level namespace, but at the time I posted the answer, this came to mind :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.