[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Data Structs in Ruby

Edipofederle Edipofederle

10/31/2008 6:35:00 PM

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.
--
Posted via http://www.ruby-....

3 Answers

Bilyk, Alex

10/31/2008 9:28:00 PM

0

Array can act as a stack as it has #pop and #last. It can also act as a que=
ue as it also has #first and #shift. You can wrap this functionality in a Q=
ueue and Stack classes to make it more explicit if you like.

-----Original Message-----
From: edipofederle@gmail.com [mailto:edipofederle@gmail.com]
Sent: Friday, October 31, 2008 11:35 AM
To: ruby-talk ML
Subject: Data Structs in Ruby

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.
--
Posted via http://www.ruby-....


Brian Candler

10/31/2008 9:34:00 PM

0

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.
--
Posted via http://www.ruby-....

Robert Klemme

11/1/2008 10:15:00 AM

0

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