Jakub Zak Jakub Zak - 4 years ago 305
Ruby Question

Ruby Queue to Array

Is there a way other than this to get all the elements of the queue object?

def method queue
array = []
until queue.empty? do
array << queue.pop
end
array
end


I was trying something like this:

def method queue
Array(until queue.empty? do queue.pop end)
end


But that returns an empty array.

Answer Source

Using Integer#times, Enumerable#map:

def to_a queue
  queue.size.times.map { queue.pop }
end
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download