سؤال

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.

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

You could look into gems like DelayedJob or Resque.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top