装入两张红宝石模块/宝石名称相同红宝石、两张、模块、名称

2023-09-11 08:19:22 作者:年华何日不离伤

我想用两个宝石访问亚马逊网络服务(AWS)。一个是亚​​马逊的AWS-SDK,另一个是亚马逊EC2。我使用的第二个作为AWS-SDK不包括亚马逊服务CloudWatch的部分。

I'm trying to use two Gems to access Amazon Web Services (AWS). One is the Amazon 'aws-sdk', the other is 'amazon-ec2'. I'm using the second as the aws-sdk does not cover the cloudwatch section of the amazon services.

问题是,无论是加载到同一个命名空间。

The issue is that both load into the same namespace.

require 'aws-sdk'         # aws-sdk gem
require 'AWS'             # amazon-ec2 gem

config = {:access_key_id => 'abc', :secret_key => 'xyz'}

# start using the API with aws-sdk
ec2 = AWS::EC2.new(config)  

# start using the API for anazon-ec2
cw = AWS::Cloudwatch::Base.new(config) 

现在这种理解上的最后一行抛出一个错误的AWS模块指向第一个必需的库,在这种情况下,AWS-SDK。

Now this understandably throws an error on the last line as the AWS module is pointing at the first required library, in this case aws-sdk.

NameError: uninitialized constant AWS::Cloudwatch

那么,有没有可能对我来说,这些加载到另一个命名空间中的一个?类似于

So, is it possible for me to load one of those into another namespace? Something like

require 'aws-sdk', 'AWS_SDK'
require 'AWS', 'AWS_EC2'

ec2 = AWS_SDK::EC2.new(config)  
cw = AWS_EC2::Cloudwatch::Base.new(config) 

还是有另一种伎俩,我可以在这里使用?

Or is there another trick I could use here?

感谢

推荐答案

在Ruby中,模块具有相同名称不同的宝石不会相互取代。如果一个宝石工具

In Ruby, modules with the same name from different gems don't replace each other. If one gem implements

module AWS
  class Foo
  end
end

和其它工具

module AWS
  class Bar
  end
end

和你需要他们两个,你最终会与同时包含类Foo和Bar类(一个AWS模块,除非第二个做一些事情很麻烦,喜欢明确取消定义任何已有present模块中,定义之前自己的东西,这是非常不可能的)。只要第二个宝石没有的重新定义的在首批创业板的任何方法(或尝试使用一个模块为一类或反之),他们应该都可以。我想你可能找错了解决方案。

and you require them both, you will end up with an AWS module that contains both a class Foo and a class Bar (unless the second does something really tricky like explicitly undefining anything already present in the module, before defining its own stuff, which is very unlikely). As long as the second gem doesn't redefine any methods in the first gem (or attempts to use a module as a class or vice versa), they should both work fine. I think you may be looking for the wrong solution.

编辑: 而事实上,有什么对我来说发生(在一个环境中,只有这些宝石present(AWS-SDK 1.2.3和亚马逊EC2 0.9.17)和c你上面列出的确切$ C $)正是如此:

And in fact, what happens for me (in an environment with only these gems present (aws-sdk 1.2.3 and amazon-ec2 0.9.17) and the exact code you listed above) is exactly that:

.rvm/gems/ree-1.8.7-2011.03@ec2/gems/amazon-ec2-0.9.17/lib/AWS/EC2.rb:2: EC2 is not a module (TypeError)

会不会是一个错误被吞噬的地方,而且模块AWS :: CloudWatch的还没有被定义,仅仅是因为创业板的初始化都被打乱?

Could it be that an error gets swallowed somewhere and that the module AWS::Cloudwatch hasn't been defined, simply because the initialization of the gem goes awry?

 
精彩推荐
图片推荐