# Start and stop a ruby loop ;(

**URL:** <https://www.ruby-forum.com/t/start-and-stop-a-ruby-loop/127575>\
**Category:** Ruby\
**Created:** [January 30, 2008, 2:44pm UTC](https://www.ruby-forum.com/t/start-and-stop-a-ruby-loop/127575 "2008-01-30T14:44:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [January 30, 2008, 2:44pm UTC](https://www.ruby-forum.com/t/start-and-stop-a-ruby-loop/127575/1 "2008-01-30T14:44:45Z")

</div>

Hi all,

I have the following loop

class myFirstClass  
is\_loop = 0

def start\_my\_loop  
for is\_loop \< 1  
puts ‘Im inside the loop…’  
end  
end

end

i can start above class’s loop by using following…

require ‘myFirstClass’  
class mySecondClass  
oFirst = myFirstClass.new  
oFirst.start\_my\_loop  
end

and i need a way to stop the above loop from calling another method.  
Like…  
calling a nother method to make the ‘is\_loop’ to 1

but the problem is when i use the  
myFirstClass.new --\> is\_loop to 0

so can anyone tell me a way to do this. I want to start the loop from  
one method and stop it from another. (Or what is the way to do this)

I’m sorry if i’m doing something wrong coz i’m nwe to ruby

thankx in advance

cheers  
sameera

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [January 31, 2008, 2:11pm UTC](https://www.ruby-forum.com/t/start-and-stop-a-ruby-loop/127575/2 "2008-01-31T14:11:55Z")

</div>

Hi Wyatt,

Thank you very much for your great help. It has lots of things to learn  
for a beginner like me. So i will defiantly try these.

And thankx again for making it so clear

have a nice day

cheers  
sameera

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [January 31, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/start-and-stop-a-ruby-loop/127575/3 "2008-01-31T14:56:50Z")

</div>

2008/1/30, Wyatt G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> a = MyFirstClass.new  
> a.start\_my\_loop do  
> if rand(5) == 3  
> false  
> else  
> true  
> end  
> end

Why make it more complicated than necessary?

a = MyFirstClass.new  
a.start\_my\_loop do  
rand(5) == 3  
end

But I guess the OP rather wanted a concurrency solution.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [January 30, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/start-and-stop-a-ruby-loop/127575/4 "2008-01-30T16:10:55Z")

</div>

You’re almost there. First of all, in Ruby, you don’t declare your  
instance variables outside of methods. You just set them inside of a  
method, and they get automatically declared. For example:

class Foo

def bar  
@baz = 3  
end

def qux  
@quux = 4  
end

def show\_me  
p @baz  
p @quux  
end

end

If you say:

a = Foo.new  
a.show\_me

It should print:

nil  
nil

Because the object’s instance variables of @baz and @quux haven’t been  
initialized. If you say:

a = Foo.new  
a.bar  
a.show\_me

It should print:

3  
nil

because when the bar method was called, it initialized the @baz  
variable to 3. Likewise, if you say:

a = Foo.new  
a.bar  
a.qux  
a.show\_me

You will get:

3  
4

Also, it looks like you want to use a while loop instead of a for  
loop. So for your example, you could do this:

class MyFirstClass

def start\_my\_loop  
while !@stop\_loop  
puts ‘Im inside the loop…’  
end  
end

def stop\_my\_loop  
@stop\_loop = true  
end

end

Notice that when you call start\_my\_loop, if @stop\_loop hasn’t yet been  
initialized, its value is nil. This means that it will begin  
executing the loop. If you wanted to explicitly set the value of  
@stop\_loop from the get-go, then use a constructor:

class MyFirstClass

def initialize  
@stop\_loop = false  
end

def start\_my\_loop  
while !@stop\_loop  
puts ‘Im inside the loop…’  
end  
end

def stop\_my\_loop  
@stop\_loop = true  
end

end

So now you can use this code like this:

a = MyFirstClass.new  
a.start\_my\_loop  
a.stop\_my\_loop

However, this will not work. Once the program enters the loop, it  
stays there. You need some way to pass control of the program to  
outside of the loop, otherwise, you’ll be looping forever. One way to  
do this is to use multiple threads. Another way is to use blocks,  
like this:

class MyFirstClass

def start\_my\_loop  
while yield  
puts ‘Im inside the loop…’  
end  
end

end

a = MyFirstClass.new  
a.start\_my\_loop do  
if rand(5) == 3  
false  
else  
true  
end  
end

Every time the program gets to the yield statement, it calls the block  
that you passed to the start\_my\_loop method. This block randomly  
returns true or false. If it returns true, the loop is executed  
again.

I hope this was helpful!

Cheers,  
Wyatt G.
