From: "Jesús Gabriel y Galán" Date: 2007-10-23T23:56:40+09:00 Subject: Re: pop/push, shift/unshift On 10/23/07, Bob Hutchison wrote: > On 23-Oct-07, at 10:37 AM, David A. Black wrote: > > On Tue, 23 Oct 2007, Jes¨Bs Gabriel y Gal¡¤n wrote: > > > >> Fair enough, although the idea was to use such an object using just > >> queue semantics, so everybody knows what enqueue/dequeue do. Maybe > >> aliasing them in Array doesn't make sense: what about having those > >> alias in a class Queue < Array or in the instance of the array you > >> plan to use just as a queue? > >> > >> queue = [] > >> class << queue > >> alias :enqueue :push > >> alias :dequeue :shift > >> end > > > > Now you're talking :-) You could even wrap it up like this: > > > > module Queue > > def self.extend_object(o) > > class << o > > alias enqueue push > > alias dequeue shift > > end > > end > > end > > > > q = [].extend(Queue) > > > > (I'm a big #extend fan.) > > > > > > David > > > > > > This is potentially a troublesome implementation. I believe that the > queue is going to grow and grow. See my other messages about the > behaviour of un/shift. As I understand this is only a problem with shift, and not with pop, I propose this: module Queue def self.extend_object(o) class << o alias enqueue unshift alias dequeue pop end end end q = [].extend(Queue) :-) Jesus.