From: "Jesús Gabriel y Galán" Date: 2007-10-25T00:34:20+09:00 Subject: Re: pop/push, shift/unshift On 10/24/07, Todd Benson wrote: > On 10/24/07, Jesús Gabriel y Galán wrote: > > class Queue > > def initialize > > @buffer = [] > > end > > > > def enqueue(element) > > @buffer.unshift(element) > > end > > > > def dequeue > > @buffer.pop > > end > > > > def empty? > > @buffer.empty? > > end > > end > > > > But, there's already a Queue implementation in ruby core, so I would > > use that, although that class is aimed at synchronizing threads so I > > might implement a simple queue like the above at some point if I need > > simple queue semantics. > > I would corresponding class for Stack, with push/pop being delegated > > to array's push and pop :-). Or just use an array if the fact of > > having more methods than a stack is not a problem :-). > > > > Jesus. > > You want to -- as mentioned elsewhere in this thread -- push it in the > front and take it out the back. Interesting. I guess it all comes > down to what direction you want to go. I like to see the procession > moving towards the zeroth position and not to some indexth position. The truth is that I don't mind in which direction to go, because I only want queue semantics, and that would be part of an implementation detail. I changed in my example to unshift/pop because of the recommendations made above in this thread about not using shift, but other than that I wouldn't mind. Jesus.