Re: I'm confused about Ruby.2.5.3 Method#arity for Dir.entries and Dir#entries

"Frank J. Cameron" <[email protected]> Wed, 16 Oct 2019 22:37:32 -0400
Newsgroups gmane.comp.lang.ruby.general
Message-ID <83fd0c840789b78e13026eddfb276edc.squirrel@squirrelmail.tuffmail.net>
Colin Bartlett wrote:
> Method#arity: Returns an indication of the number of arguments accepted
> by a method. Returns a nonnegative integer for methods that take a fixed
> number of arguments. For Ruby methods that take a variable number of
> arguments, returns -n-1, where n is the number of required arguments. ...
>
> In the following: 1. Why isn't MRI Dir.method(:entries).arity == 1?
>
> Dir.method(:entries).arity  #=> -1  # implying n = 0 so can accept 0
> Dir.entries()  #=> wrong number of arguments (given 0, expected 1)

In the ... section of your quote from the docs: "[arity] For methods written
in C, returns -1 if the call takes a variable number of arguments."

Dir.entries is indeed written in C:

    dir_entries(int argc, VALUE *argv, VALUE io)

If I'm not too lost, it looks like:

    dir_entries
      -> dir_open_dir
        -> dir_s_open
          -> dir_initialize
            -> rb_scan_args

    rb_scan_args(argc, argv, "1:", &dirname, &opt);

"1:" 1 mandatory argument, keyword arguments



Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>