Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

296
Vistas
Ruby print or return specific field from object

How do I print the group_id from the returned object?

The following is returned from a function. I want to print the group_id or maybe return the group_id

 {
  :security_groups=>[
    {
      :description=>"Created By ManageIQ",
      :group_name=>"MIQ_019",
      :ip_permissions=>[
        {
          :from_port=>22,
          :ip_protocol=>"tcp",
          :ip_ranges=>[
            {
              :cidr_ip=>"0.0.0.0/0",
              :description=>nil
            }
          ],
          :ipv_6_ranges=>[],
          :prefix_list_ids=>[],
          :to_port=>22,
          :user_id_group_pairs=>[]
        }
      ],
      :owner_id=>"943755119718",
      :group_id=>"sg-0c2c5f219f1bafc1a",
      :ip_permissions_egress=>[
        {
          :from_port=>nil,
          :ip_protocol=>"-1",
          :ip_ranges=>[
            {
              :cidr_ip=>"0.0.0.0/0",
              :description=>nil
            }
          ],
          :ipv_6_ranges=>[],
          :prefix_list_ids=>[],
          :to_port=>nil,
          :user_id_group_pairs=>[]
        }
      ],
      :tags=>[],
      :vpc_id=>"vpc-d817c1b3"
    }
  ],
  :next_token=>nil
}

This is the function: I want to return security_group.group_id

def describe_security_group (
  group_name
 )  
  ec2 = get_aws_client
  security_group = ec2.describe_security_groups(
    filters: [
      {name: 'group-name', values: [ group_name ]}]
    )
  puts "Describing security group '#{group_name}' with ID " \
    "'#{security_group}'"
  return security_group
rescue StandardError => e
  puts "Error describing security group: #{e.message}"  
  return 
end
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

So, returning value seems like a hash, or you can make it hash exactly.

For case with one-element array you can simple use ruby dig method.

And according to your datum and comment below we can access needed element like this:

# from your ec2 api call
security_group  = ec2.describe_security_groups(...)

# Result value is stored in `security_group` variable,
# and looks exactly like hash below
{
  :security_groups=>[
    {
      :description=>"Created By ManageIQ",
      :group_name=>"MIQ_019",
      :ip_permissions=>[
        {
          :from_port=>22,
          :ip_protocol=>"tcp",
          :ip_ranges=>[
            {
              :cidr_ip=>"0.0.0.0/0",
              :description=>nil
            }
          ],
          :ipv_6_ranges=>[],
          :prefix_list_ids=>[],
          :to_port=>22,
          :user_id_group_pairs=>[]
        }
      ],
      :owner_id=>"943755119718",
      :group_id=>"sg-0c2c5f219f1bafc1a",
      :ip_permissions_egress=>[
        {
          :from_port=>nil,
          :ip_protocol=>"-1",
          :ip_ranges=>[
            {
              :cidr_ip=>"0.0.0.0/0",
              :description=>nil
            }
          ],
          :ipv_6_ranges=>[],
          :prefix_list_ids=>[],
          :to_port=>nil,
          :user_id_group_pairs=>[]
        }
      ],
      :tags=>[],
      :vpc_id=>"vpc-d817c1b3"
    }
  ],
  :next_token=>nil
}

# And this is a target value, that you can store in another one,
# return from method or simply print to output
security_group.dig(:security_groups)
              .try(:[], 0)
              .dig(:group_id)

=> "sg-0c2c5f219f1bafc1a"

But if you need to search in array with multiple elements, methods from Ruby's Enumerable module could be helpful (like select or reject).

UPDATE with OpenStruct, if you prefer such method calls with dot notation:

json = security_group.to_json

os = JSON.parse(json, object_class: OpenStruct)

os.security_groups.first.group_id
=> "sg-0c2c5f219f1bafc1a"
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda