# Trouble waiting X minutes

**URL:** <https://www.ruby-forum.com/t/trouble-waiting-x-minutes/191573>\
**Category:** Ruby\
**Created:** [July 14, 2010, 5:27pm UTC](https://www.ruby-forum.com/t/trouble-waiting-x-minutes/191573 "2010-07-14T17:27:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![teedubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@teedubb](https://www.ruby-forum.com/u/teedubb)\
**Post date:** [July 14, 2010, 5:27pm UTC](https://www.ruby-forum.com/t/trouble-waiting-x-minutes/191573/1 "2010-07-14T17:27:59Z")

</div>

I am making a constant network connection that is feeding me  
information, i want to take that information and place into nested /  
two-dimensional array for 1 minute, below is the code that’s not  
working.

class Test

def initialize  
@junk = Array.new  
@opensocket = TCPSocket.new(‘localhost’, 8080)  
read  
end

def read  
while @lines = @opensocket.gets  
FasterCSV.parse(@lines) do |@row|  
pass  
end  
end  
end

def pass  
@junk \<\< @row  
sleep(60)  
p @junk  
end  
end

enable = Test.new

So after I sleep for 1 minute you would think the @junk two dimensional  
array has filled up but it hasn’t, what’s the best way to solve this  
problem?

---

<div class="post-metadata">

**Author:** ![teedubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@teedubb](https://www.ruby-forum.com/u/teedubb)\
**Post date:** [July 14, 2010, 8:29pm UTC](https://www.ruby-forum.com/t/trouble-waiting-x-minutes/191573/2 "2010-07-14T20:29:51Z")

</div>

On Jul 14, 2010, at 8:28 AM, Tee D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am making a constant network connection that is feeding me  
> information, i want to take that information and place into nested /  
> two-dimensional array for 1 minute, below is the code that’s not  
> working.

[snip]

> So after I sleep for 1 minute you would think the @junk two dimensional  
> array has filled up but it hasn’t, what’s the best way to solve this  
> problem?

Well, _I_ wouldn’t think that would be the case… Sleep suspends the  
execution of your program until the timer expires, so nothing else is  
happening during that time.

There are lots of ways that you could do what you want… Perhaps the  
“simplest” way would be to capture the time in a variable before you  
start, and check it after every row. If a minute has gone by, stop.  
However, that won’t be terribly accurate and you won’t guarantee exactly  
one minute.

Another option would be to start another thread to manage the timing,  
but I’m not sure right off the top of my head what that would look like.

A third option might be to use the Timeout class, but that is sort of a  
hack. You should read up on these options and pick one that works for  
you.

ri Thread  
ri Timeout

Ben

---

<div class="post-metadata">

**Author:** ![teedubb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@teedubb](https://www.ruby-forum.com/u/teedubb)\
**Post date:** [July 14, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/trouble-waiting-x-minutes/191573/3 "2010-07-14T22:54:57Z")

</div>

Tee D. wrote:

> below is the code that’s not  
> working.

Your program doesn’t have any threads, so it will execute in lock-step.  
That is, when you enter pass, you will sleep for 60 seconds there,  
before returning.

Try adding some puts statements to see what’s going on, e.g.

> class Test
> 
> def initialize

puts “start initialize”

> @junk = Array.new  
> @opensocket = TCPSocket.new(‘localhost’, 8080)

puts “socket open”

> read

puts “read has returned, done initialize”

> end
> 
> def read

puts “start read”

> while @lines = @opensocket.gets  
> FasterCSV.parse(@lines) do |@row|

puts “Read line #{@row.inspect}, going to pass”

> ```
> pass
> 
> ```

puts “pass has returned”

> ```
> end
> 
> ```
> 
> end

puts “done read”

> end
> 
> def pass

puts “starting pass”

> @junk \<\< @row

puts “sleeping for 60…”

> sleep(60)  
> p @junk  
> end  
> end
> 
> enable = Test.new

You could alternatively try to use a debugger to single-step the  
program, but generally I find the hassle of learning to drive the  
debugger to be higher than putting the debug statements in.
