tags:

views:

68

answers:

2

How to produce delay in ruby?

I used sleep statement but it didn't give me what I want.

puts "amit"
sleep(10)
puts "scj"

I want it to first print amit, then a delay of 10 seconds, then print scj.

But in above case what happens is it will pause for 10 seconds and then it will print amit and scj together. I don't want that.

I hope you got what I want to say.

+3  A: 

Call $stdout.flush before the call to sleep. The output is probably buffered (although usually output is only line-buffered so puts, which produces a newline, should work without flushing, but apparently that's not true for your terminal).

sepp2k
nathanvda why r u saying run ur script from command line not from SciTE .actully i m running from SCITE itself i want to get delay from SCite itself but how to do that plzz help me
amit singh tomar
nd plzz let me know abt ur exmple u gave above puts 'bla' sleep(10) and after that what is this continue1??
amit singh tomar
+1  A: 

I can't reproduce this. From a console, this does exactly what you'd expect:

puts "amit"
sleep 10
puts "scj"

(Ruby 1.8.6 on Linux)

Can you provide a similar short but complete example which doesn't do what you want - or explain your context more?

If you're writing a web application, then the browser may well only see any data once the whole response has been written - that would explain what you're seeing. If that's the case, you'll need a different approach which would allow the initial response to be written first, and then make the browser make another request. The delay could be at the server or the client, depending no the scenario.

Jon Skeet
actully jon what i want is simple i want to get delay between two these two statement but using sleep what happening the programme execution starts from sleep statement itself that i don't want i want flow of execution start from puts"amit" and then i want 10 sec delay and final statement ... thats it i want
amit singh tomar