Pergunta

I have a problem with the following piece of code using Fog to get a list of servers from Rackspace - I am concatenating servers new generation with servers old generation.

def servers()
  servers = @service.servers
  servers_old = @service_old.servers
  size = servers.length + servers_old.length # line 4
  all_servers = servers + servers_old # line 5
  servers = all_servers.sort_by { |k| k.name}
  return servers
end

If the fourth line is commented out, the method is returning only the service.servers array. Concatenation in the fifth line does not happen. It seems to me that the arrays service.servers and service_old.servers are somehow lazy until I explicitly ask for them.

With the uncommented fourth line, the method is returning concatenated arrays, which is what I expect. If these arrays are lazy is there any recommended method for evaluating them? Right now I am just using length but I don't really need it. Any hint/link for some specific documentation?

Foi útil?

Solução

.all is your friend here. Fog collections generally have an all method that fixes the issue with lazy loading that you are having. Instead of @service.servers, use this:

servers = @service.servers.all

and

servers_old = @service_old.servers.all
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top