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.
Array.new(queue.size) { queue.pop }
Using Integer#times
, Enumerable#map
:
def to_a queue
queue.size.times.map { queue.pop }
end
I would write something like:
def method(queue)
[].tap { |array| array << queue.pop until queue.empty? }
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With