# While loop end action

**URL:** <https://www.ruby-forum.com/t/while-loop-end-action/191444>\
**Category:** Ruby\
**Created:** [July 12, 2010, 8:45pm UTC](https://www.ruby-forum.com/t/while-loop-end-action/191444 "2010-07-12T20:45:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [July 12, 2010, 8:45pm UTC](https://www.ruby-forum.com/t/while-loop-end-action/191444/1 "2010-07-12T20:45:33Z")

</div>

I am trying to write a script that when certain circumstances arise it  
creates an object instance that adds some info to a temporary array for  
a given time. When the given time is up I would like to perform a couple  
actions and print the array. After that the array and object instance  
will no longer be needed, so I would like to ensure the obj is freed  
from memory. I am pretty new to coding and to Ruby, what is the best  
way to accomplish this. I thought it would be a while loop but I am  
confused as to how to perform an action once my counter or time has  
reached it’s end. For example, where can I put the ending action?

class Blah  
def initialize  
@myarray = Array.new  
@init\_time = Time.now.to\_f  
@twomin = @init\_time + 120  
end

def add\_to\_array(x)  
while Time.now.to\_f \< @twomin  
@myarray \<\< x  
end  
#pp @myarray.inspect  
end

end

Since the while loop has a definite end. Does this end the object’s  
instance, I suspect no and I need to put some die statement in there or  
delete the object from outside the object?

If I do an if/else loop than it will continue to run I suspect too.  
if Time.now.to\_f \< @twomin  
@myarray \<\< x  
else  
pp @myarray.inspect  
end

How can I properly run some actions or a method after my counter/time  
has reached it’s end and then delete the obj instance?

Thanks

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [July 13, 2010, 7:09am UTC](https://www.ruby-forum.com/t/while-loop-end-action/191444/2 "2010-07-13T07:09:00Z")

</div>

John Crichton wrote:

> How can I properly run some actions or a method after my counter/time  
> has reached it’s end and then delete the obj instance?
> 
> Thanks

since no one has responded I’ll make a quick try…

1. you can always do:

myarray = nil

1. Your variables and the print can be local variables inside the  
method. Use @var only for instance level data. in your case, you do not  
need the data outside of the method.

def method  
array = []

# or array = Array.new

while …  
array \<\< “str”  
end  
puts array.join(",")  
end

In the above example, array is local data and will be released soon  
after method is over.

If array is very large, and your method is really long, and you do not  
need array after printing, then you can do “array = nil” after its last  
use.  
HTH.

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [July 13, 2010, 7:15am UTC](https://www.ruby-forum.com/t/while-loop-end-action/191444/3 "2010-07-13T07:15:12Z")

</div>

John Crichton wrote:

> ```
> @init_time = Time.now.to_f
> @twomin = @init_time + 120
> 
> ```

> Thanks

Is there any specific reason you are doing to\_f. You can use to\_i to get  
seconds since epoch too.

You can also just say:

```
 Time.now + 120

```

This page might help you:

> **[Ruby - Date & Time](http://www.tutorialspoint.com/ruby/ruby_date_time.htm)**
>
> Ruby Date Time - The Time class represents dates and times in Ruby. It is a thin layer over the system date and time functionality provided by the operating system. This class may be unable on your system to represent dates before 1970 or after 2038.

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [July 13, 2010, 5:13pm UTC](https://www.ruby-forum.com/t/while-loop-end-action/191444/4 "2010-07-13T17:13:20Z")

</div>

Thanks Kumar, I appreciate it and no there was no reason I was using  
to\_f over to\_i other than inexperience.

Thanks again.

R… Kumar 1.9.1 OSX wrote:

> John Crichton wrote:
> 
> > ```
> > @init_time = Time.now.to_f
> > @twomin = @init_time + 120
> > 
> > ```
> 
> > Thanks
> 
> Is there any specific reason you are doing to\_f. You can use to\_i to get  
> seconds since epoch too.
> 
> You can also just say:
> 
> ```
> Time.now + 120
> 
> ```
> 
> This page might help you:  
> [Ruby - Date & Time](http://www.tutorialspoint.com/ruby/ruby_date_time.htm)
