UberGM
UberGM

Reputation: 99

Do multiple tasks at once in ruby

I have an issue with the following code. What I want to do is run the while statement in the background, while the program receives the users input

while @adv == 1 do
  @infected += 1
  sleep 1
end
puts "commands: infect, refresh"
uin = gets.chomp
if uin == "infect"
  input
elsif uin == "refresh"
  start
end

Upvotes: 1

Views: 49

Answers (1)

bkahler
bkahler

Reputation: 365

Try spinning up a new thread to run the while loop. Something like :

Thread.new do 
 while @adv == 1 do
   @infected += 1
   sleep 1
 end
end

This will allow the loop to run as the rest of the code is executed.

Upvotes: 1

Related Questions