How to do rand(0.2..0.5) after Deprecated ::sleep?

sleep rand(0.2..0.5)

Warning: Deprecated ::sleep. Use `::sleep(Time::Span)` instead

A total of 1 warnings were found.

Why we deprecated it? how to do above same functionality more easier?

AFAIK, i have to do like following:

sleep Time::Span.new(nanoseconds: (rand(0.2..0.5)*1000000000).to_u64)

I don’t even know what a nanosecond is in seconds before this change, I don’t want to count how many zeros I need to add. Why Time::Span not add support for ms, μs before deprecarted? or support use Number instead of Int when passing TimeSpan.new(*, seconds: Int)?

ping Deprecate `::sleep(Number)` by HertzDevil · Pull Request #14962 · crystal-lang/crystal · GitHub

sleep rand(0.2..0.5).seconds

:grinning:

2 Likes

Oh, we can replace it with:

sleep rand(0.2..0.5).seconds

Yes, when I looked at the examples in the above PR, I realized I made a mistake.