# Data Structs in Ruby

**URL:** <https://www.ruby-forum.com/t/data-structs-in-ruby/152247>\
**Category:** Ruby\
**Created:** [October 31, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/data-structs-in-ruby/152247 "2008-10-31T19:36:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![edipofederle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@edipofederle](https://www.ruby-forum.com/u/edipofederle)\
**Post date:** [October 31, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/data-structs-in-ruby/152247/1 "2008-10-31T19:36:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![edipofederle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@edipofederle](https://www.ruby-forum.com/u/edipofederle)\
**Post date:** [October 31, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/data-structs-in-ruby/152247/2 "2008-10-31T22:29:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![edipofederle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@edipofederle](https://www.ruby-forum.com/u/edipofederle)\
**Post date:** [October 31, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/data-structs-in-ruby/152247/3 "2008-10-31T22:34:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![edipofederle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e4f/32.png) [@edipofederle](https://www.ruby-forum.com/u/edipofederle)\
**Post date:** [November 1, 2008, 11:15am UTC](https://www.ruby-forum.com/t/data-structs-in-ruby/152247/4 "2008-11-01T11:15:31Z")

</div>

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
