简体   繁体   English

从红宝石AWS instanceCollection对象获取instance_id数组

[英]Getting array of instance_id from ruby AWS instanceCollection object

I'm new to ruby (also to AWS) and trying to create and populate ELB after creating EC2 instances 我是ruby(也是AWS)的新手,并在创建EC2实例后尝试创建并填充ELB

instances = ec2.instances.create(...)

I now need an array of theirs ID I need something like this (in c# ) 我现在需要他们的ID数组,我需要像这样的东西(在c#

list<string> ids = new list<string>

    Foreach (instance in instances)
    {
      ids.add(instance.id);
    }
    string [] id_Array = ids.toarray();

what is the fastest way to do it in Ruby 在Ruby中最快的方法是什么

You can call map on your instances collection to use the .id method to create an array of instance ids . 您可以在instances集合上调用map ,以使用.id方法创建instance ids数组。

instances = ec2.instances.create(...)
ids = instances.map(&:id)

References: 参考文献:

AWS::EC2::Instance#id AWS EC2 :: ::#实例ID

Array#map 阵列#地图

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM