Navindra Umanee wrote:
> Sam Roberts <sroberts / uniserve.com> wrote:
> > Can anybody give me any hints as to what I should be looking for?
What
> > can cause sleep(0) to wake up?
>
> The documentation is wrong.
>
> sleep(0) returns immediately.  sleep with *zero* arguments sleeps
> forever.
>
> static VALUE
> rb_f_sleep(argc, argv)
>     int argc;
>     VALUE *argv;
> {
>     int beg, end;
>
>     beg = time(0);
>     if (argc == 0) {
>         rb_thread_sleep_forever();
>     }
>     else if (argc == 1) {
>         rb_thread_wait_for(rb_time_interval(argv[0]));
>     }
>     else {
>         rb_raise(rb_eArgError, "wrong number of arguments");
>     }
>
>     end = time(0) - beg;
>
>     return INT2FIX(end);
> }
>
> Cheers,
> Navin.

Why are arguments being counted manually?  It's less code, less error
prone, and doesn't force you to explicitly raise an ArgumentError if
the argument count is wrong.

Refactored:

/*
 * :call-seq:
 *    sleep(numeric=nil)
 *
 * Suspends the current thread for +numeric+ seconds (which may be a
 * Float with fractional seconds.  Returns the actual number of seconds
 * slept, rounded, which may be less than what was asked for if the
 * thread was interrupted by a SIGALARM or if another thread calls
 * Thread#run.
 *
 * If no argument is provided then it will sleep forever.
 */
static VALUE
rb_f_sleep(argc, argv)
    int argc;
    VALUE *argv;
{
    int beg, end;
    VALUE rbNum;

    beg = time(0);

    rb_scan_args(argc,argv,"01",&rbNum);

    if(NIL_P(rbNum)){
       rb_thread_sleep_forever();
    }
    else{
       rb_thread_wait_for(rb_time_interval(rbNum));
    }

    end = time(0) - beg;

    return INT2FIX(end);
}