Forum: Ruby Data Structs in Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5d052a386fa13aceec719a8f37a6231c?d=identicon&s=25 Edipofederle Edipofederle (edipofederle)
on 2008-10-31 19:36
Hello,


How would the implementation of such a stack or queue, and Ruby, already
has something "ready" to handle ...

Thakns.

I sorry but my english is not very good.
58d1114fe29aab2d433231a722dd2034?d=identicon&s=25 Bilyk, Alex (Guest)
on 2008-10-31 22:29
(Received via mailing list)
Array can act as a stack as it has #pop and #last. It can also act as a
queue as it also has #first and #shift. You can wrap this functionality
in a Queue and Stack classes to make it more explicit if you like.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-31 22:34
Bilyk, Alex wrote:
> Array can act as a stack as it has #pop and #last. It can also act as a
> queue as it also has #first and #shift. You can wrap this functionality
> in a Queue and Stack classes to make it more explicit if you like.

Included in the Ruby standard library:

irb(main):001:0> require 'thread'
=> true
irb(main):002:0> q = Queue.new
=> #<Queue:0xb7d753e0>
irb(main):003:0> q.push "abc"
=> #<Queue:0xb7d753e0>
irb(main):004:0> q.pop
=> "abc"

This queue object is thread-safe. There is also SizedQueue, which blocks
pushes when the queue reaches a certain size, until another element has
been popped.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-11-01 11:15
(Received via mailing list)
On 31.10.2008 22:27, Bilyk, Alex wrote:
> Array can act as a stack as it has #pop and #last.

It even has #push.

> It can also act as a queue as it also has #first and #shift.

You can use #push and #shift or #unshift and #pop for queue behavior.
Plus there is Queue as Brian mentioned.

Cheers

  robert
This topic is locked and can not be replied to.