Question: 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);

```

Answer: 
Looks overkill for this task, but you can use [delayed\_job](https://github.com/tobi/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 }

```
Why not just store the time until it is active? 

Then there is no need to block, callback or run anything async at all. When you store `active_until` in a datetime column in a database model, then it allows you keep track of changes and keep the current `active` status even over several different requests to the app:

```
def active
  active_until.blank? || Time.current < active_until
end

def deactivate
  update_columns(active_until: 2.minutes.since)
end

```