Search code examples
rubyinternals

What is the private method Object#select?


Like many others, I saw the exception

private method 'select' called for nil:NilClass (NoMethodError)

go by. What struck me as odd is that it didn't say undefined method 'select' for nil:NilClass (NoMethodError). There was indeed such a method, and it was private!

What is this method?

I've found that it's defined on Object, and not just NilClass, but it's not defined on BasicObject. By using #send, I've found that it takes 1..4 arguments. The first three must be an array (or nil). And the last one must be convertible to a "time interval". In any case, when it doesn't throw an argument error, it hangs.

It's not in the ruby documentation because it's private. I don't really know how to read the C source code, and I haven't been able to find anything suggestive there. I expected to see something like the following line in object.c;

rb_define_private_method(rb_cObject, "select", select, 0);

But alas, I cannot.

What is this method?


Solution

  • It's Kernel#select. Per the docs for the Kernel module:

    The Kernel module is included by class Object, so its methods are available in every Ruby object.

    And in case you're curious what select does:

    select(read_array [, write_array [, error_array [, timeout]]]) → array or nil

    Calls select(2) system call. It monitors given arrays of IO objects, waits until one or more of IO objects are ready for reading, are ready for writing, and have pending exceptions respectively, and returns an array that contains arrays of those IO objects.

    You can read more background on why Object includes Kernel in the answer to this question: Why does the Ruby module Kernel exist?