Question

I want to know how to execute a function after a certain amount of time has passed. The user will enter a duration, say 30 minutes, and after 30 minutes they will be given a message, along with other code being done. I am new to Ruby, and can't figure out the best way to do it.

Était-ce utile?

La solution

If you don't want to block IO you can use threads:

time = gets.to_i # time in seconds

Thread.new do
  sleep time
  # your code here
end

Or just:

time = gets.to_i # time in seconds
sleep time
# your code here

Autres conseils

You could look into gems like DelayedJob or Resque.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top