加载两个同名的 Ruby 模块/Gems [英] Load two Ruby Modules/Gems with the same name

查看:27
本文介绍了加载两个同名的 Ruby 模块/Gems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个 Gems 来访问 Amazon Web Services (AWS).一个是亚马逊aws-sdk",另一个是amazon-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 中,来自不同 gem 的同名模块不会相互替换.如果一颗 gem 实现

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 模块(除非第二个模块做了一些非常棘手的事情,比如在定义自己的东西之前明确地取消定义模块中已经存在的任何东西,这是非常不可能的).只要第二个 gem 没有重新定义第一个 gem 中的任何方法(或者尝试将模块用作类,反之亦然),它们都应该可以正常工作.我认为您可能正在寻找错误的解决方案.

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.

事实上,对我来说(在只有这些 gems(aws-sdk 1.2.3 和 amazon-ec2 0.9.17)和上面列出的确切代码的环境中)正是这样:

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 尚未定义,仅仅是因为 gem 的初始化出错?

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?

这篇关于加载两个同名的 Ruby 模块/Gems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆