Rishav
Rishav

Reputation: 4078

Trigger a Loop to pause and continue when space bar is pressed

I want to pause the running loop when i hit space and resume it again on doing so. How do i do it?

require 'rubygems'
require 'rest-client'

URL="some url here"
data1 = 0
data2 = 0
sign = ["up","down","equal"]
i=3

while true
    response = RestClient.get(URL)
    arr = (response.body).split(/"/)
    data2=arr[9].to_i
    if data2>data1
        i = 0
    elsif data2<data1
        i = 1
    else data2 == data1
        i = 2
    end
    puts "#{arr[9]}   #{sign[i]}"
    data1=arr[9].to_i                       #marker<<<---Here
end

Simpler saying i want the loop to come to the marker and not run again till space is pressed.

Edit
I tried putting a puts there but obviously it pauses there and waits for me to give an input every time. Please try to make things as less complex as possible. I am kind of a beginner.

Upvotes: 0

Views: 248

Answers (1)

av501
av501

Reputation: 6729

Use threads? This is not a direct answer but will give you a picture of how to use them.

$key_hit = false
t1 = Thread.new{

loop{
        puts "Hello"
        break if $key_hit
    }
}

t2 = Thread.new {
    x = gets
    $key_hit = true
}
t1.join
t2.join

puts "Done, exiting"

Upvotes: 1

Related Questions