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

Colin Bartlett <[email protected]> Mon, 7 Oct 2019 14:53:36 +0100
Newsgroups gmane.comp.lang.ruby.general
Message-ID <CAKJgJc2WQ2WSwbw-uEOpxhxYpsSGJbNB-OiWBRYWWOvm7Eod0w@mail.gmail.com>
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?
2. Why isn't JRuby Dir.method(:entries).arity == -2?
3. Why isn't MRI d.method(:entries).arity == 0? (Where d = Dir.new(".").)

Thanks in advance for any help!

https://ruby-doc.org/core-2.5.3/Dir.html#method-c-entries
Public Class Methods
entries( dirname ) --> array
entries( dirname, encoding: enc ) --> array
Returns an array containing all of the filenames in the given directory.
Will raise a SystemCallError if the named directory doesn't exist.
The optional encoding keyword argument specifies the encoding of the directory.
If not specified, the filesystem encoding is used.

# OS= Microsoft Windows 7 Professional 64-bit

## MRI Ruby irb
RUBY_PLATFORM  #=> "x64-mingw32"
RUBY_VERSION   #=> "2.5.3"
String.method(:chomp)  #=> undefined method 'chomp' for #<Class:String>
s = "str*"       #=> "str*"
s.chomp()        #=> "str*"
s.chomp("?")     #=> "str*"
s.chomp("*")     #=> "str"
s.chomp("*", 1)  #=> wrong number of arguments (given 2, expected 0..1)
s.method(:length).arity  #=> 0   # as expected: needs 0 arguments
s.method(:chomp).arity   #=> -1  # as expected: needs 0 or 1 arguments
Encoding.find("filesystem")  #=> #<Encoding:Windows-1252>
enc = Encoding::ASCII_8BIT   #=> #<Encoding:ASCII-8BIT>
d = Dir.new(".")            #=> #<Dir:.>
Dir.method(:entries).arity  #=> -1  # implying n = 0 so can accept 0 arguments
d.method(:entries).arity    #=> -1  # implying n = 0 so can accept 0 arguments
Dir.entries(".")       #=> [ array of files in "." ]
Dir.entries(".", enc)  #=> wrong number of arguments (given 2, expected 1)
Dir.entries()          #=> wrong number of arguments (given 0, expected 1)
d.entries()            #=> [ array of files in "." ]
d.entries(enc)         #=> wrong number of arguments (given 1, expected 0)

## JRuby jirb
# essentially as for MRI Ruby except for:
RUBY_PLATFORM  #=> "java"
RUBY_VERSION   #=> "2.5.3"
Dir.method(:entries).arity  #=> -1  # implying n = 0 so can accept 0 arguments
d.method(:entries).arity    #=> 0   # implying n = 0 so must have 0 arguments
Dir.entries(".")         #=> [ array of files in "." ]
Dir.entries(".", enc)    #=> [ array of files in "." ]
Dir.entries(".", enc, 3) #=> wrong number of arguments (given 3, expected 1..2)
Dir.entries()            #=> wrong number of arguments (given 0, expected 1..2)

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