# Thoughts on loops and stacks

**URL:** <https://www.ruby-forum.com/t/thoughts-on-loops-and-stacks/114363>\
**Category:** Ruby\
**Created:** [September 11, 2007, 9:02pm UTC](https://www.ruby-forum.com/t/thoughts-on-loops-and-stacks/114363 "2007-09-11T21:02:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [September 11, 2007, 9:02pm UTC](https://www.ruby-forum.com/t/thoughts-on-loops-and-stacks/114363/1 "2007-09-11T21:02:36Z")

</div>

I was thinking about some of the design issues in my dice.rb script:

```
http://www2.codegnome.org:59321/scripting/showscript.php?script=dice.rb

```

and one of the major issues is queueing. I’d like to ensure that  
selecting multiple URLs doesn’t overwrite the current contents from the  
previous X selection, so I was thinking what I needed to do was  
implement a FIFO stack.

My main issues are that I don’t want to miss any X selections, but at  
the same time I don’t want ruby running in such a tight loop that it  
sucks up excessive CPU time.

Right now, I’m doing:

```
loop do;

```

# grab X selection

```
  # time-consuming WWW::Mechanize stuff with selection

```

sleep 0.3  
end

which isn’t really event driven. So, if I select several URLs in quick  
succession, only the URL in the X selection _the next time ruby looks at  
it_ gets handled.

Suggestions?

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [September 11, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/thoughts-on-loops-and-stacks/114363/2 "2007-09-11T21:36:44Z")

</div>

On Sep 11, 2007, at 1:01 PM, Todd A. Jacobs wrote:

> Suggestions?

require ‘thread’

q = Queue.new

consumer = Thread.new do  
while(( pair = q.pop ))  
event, data, \*ignored = pair  
case event  
when :something  
when :something\_else  
end  
end  
end

q.push [:somthing, ‘some data’]

…

a @ [http://drawohara.com/](http://drawohara.com/)

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [September 11, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/thoughts-on-loops-and-stacks/114363/3 "2007-09-11T23:11:31Z")

</div>

On 11.09.2007 21:01, Todd A. Jacobs wrote:

> I was thinking about some of the design issues in my dice.rb script:
> 
> ```
> http://www2.codegnome.org:59321/scripting/showscript.php?script=dice.rb
> 
> ```
> 
> and one of the major issues is queueing. I’d like to ensure that  
> selecting multiple URLs doesn’t overwrite the current contents from the  
> previous X selection, so I was thinking what I needed to do was  
> implement a FIFO stack.

There is no such thing as a “FIFO stack”. A stack has LIFO semantics -  
a queue FIFO.

> ```
> end
> 
> ```
> 
> which isn’t really event driven. So, if I select several URLs in quick  
> succession, only the URL in the X selection _the next time ruby looks at  
> it_ gets handled.
> 
> Suggestions?

If you have a single thread you can use an Array.

irb(main):022:0\> q=[]  
=\> []  
irb(main):023:0\> 5.times {|i| q.push i}  
=\> 5  
irb(main):024:0\> until q.empty?; p q.shift; end  
0  
1  
2  
3  
4  
=\> nil

Otherwise you can use a Queue as suggested by Ara. From your problem  
description it seems you rather want a multithreaded solution.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [September 12, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/thoughts-on-loops-and-stacks/114363/4 "2007-09-12T15:52:36Z")

</div>

On Sep 11, 2007, at 17:10 , Robert K. wrote:

> On 11.09.2007 21:01, Todd A. Jacobs wrote:
> 
> > I was thinking about some of the design issues in my dice.rb script:  
> > [http://www2.codegnome.org:59321/scripting/showscript.php](http://www2.codegnome.org:59321/scripting/showscript.php)?  
> > script=dice.rb  
> > and one of the major issues is queueing. I’d like to ensure that  
> > selecting multiple URLs doesn’t overwrite the current contents  
> > from the  
> > previous X selection, so I was thinking what I needed to do was  
> > implement a FIFO stack.

Did you try this?

> > array = [1,2,3,4,5]  
> > =\> [1, 2, 3, 4, 5]  
> > array.shift  
> > =\> 1  
> > array  
> > =\> [2, 3, 4, 5]  
> > array \<\< 6  
> > =\> [2, 3, 4, 5, 6]  
> > array  
> > =\> [2, 3, 4, 5, 6]  
> > array.shift  
> > =\> 2

~Wayne

s///g  
Wayne E. Seguin  
Sr. Systems Architect & Systems Administrator
