简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM