I need to change a property on a class a few minutes after it it initialized. I attempted to use sleep inside a function but it delayed execution of everything:
active = true
def deactivate
  sleep 120
  puts 'deactivate'
  active = false
end
deactivate
puts active
What I was hoping would happen is true would log out first then two minutes later deactivate would log. However, what happens is deactivate then false log out after two minutes.
In JavaScript I would do something like:
var active = true;
setTimeout(function(){
  console.log('deactivate');
  active = false;
},120000);
console.log(active);
                Using @ihaztehcodez's suggestion of a Thread I came up with the simple solution I was looking for:
Thread.new do
  sleep 120
  puts 'deactivate'
  active = false
end
His warning about persistence doesn't worry me in this case since I am using it for non-critical notifications. If the execution was critical then storing something in a database like @spickermann said or using a library like @k-m-rakibul-islam suggested would be the better solution.
Looks overkill for this task, but you can use delayed_job to run a task at a future time asynchronously.
  def deactivate
     puts 'deactivate'
     active = false
  end
  active = true
  handle_asynchronously :deactivate, :run_at => Proc.new { 2.minutes.from_now }
                        Well, it seems to me (When I try this code) the active and deactivate is out of order. So why not do this?:
   active = true
   def deactivate
     sleep 120
     puts 'deactivate'
     active = false
   end
   puts active
   deactivate
It works perfectly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With