문제

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?

도움이 되었습니까?

해결책

.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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top