lundi 28 décembre 2015

Execute code with random delay on Ruby

I want to execute my function with random delay I tried several solutions but nothing worked

My code look like this

for i in (1..10)
  puts "Love Stack"
end

I have tried this :

def every_n_seconds(n)
  loop do
      before = Time.now
      yield
      interval = n-(Time.now-before)
      sleep(interval) if interval > 0
  end
end

for i in (1..10)
  a = [1,2,3,4,5,6,7,8,9]
  a.shuffle!
  b = a[1]
    every_n_seconds(b) do
      for i in (1..10)
        puts "test"
      end
    end
end

Have you a solution ?




Aucun commentaire:

Enregistrer un commentaire