Re: I'm confused about Ruby.2.5.3 Method#arity for Dir.entries and Dir#entries
Colin Bartlett <[email protected]> Tue, 5 Nov 2019 15:57:46 +0000
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CAKtK3JpGmqaOSKK3v-phOb-Smaja+HGO6JqMr8pq1fZf9LQuWA@mail.gmail.com> |
On Wed, Oct 23, 2019 at 8:03 AM Danielle Velie <[email protected]> wrote: > Can you please explain to me what is going on sir with the program thank > you so much Danielle Lee Ramos > My apologies for my delay on responding. I hope the following helps, and isn't too little explanation. Or too much! (I don't know how much experience you have with Ruby.) I ran Interactive Ruby (irb) and JRuby Interactive Ruby (jirb) as a simple way of demonstrating what I was asking. I use comments like "#=> ..." to show the result of a command. The results shown below are for "Microsoft Windows" Ruby irb;I also show the results for JRuby jirb if that is different For more information on the method calls I made: * String#chomp: https://ruby-doc.org/core-2.5.3/String.html#method-i-chomp * String#length: https://ruby-doc.org/core-2.5.3/String.html#method-i-length * Dir.entries: https://ruby-doc.org/core-2.5.3/Dir.html#method-c-entries * Dir#entries: I couldn't find documentation for this Public Instance Method; I don't know why not. * Encoding: https://ruby-doc.org/core-2.5.3/Encoding.html # First I give information about the Ruby version I'm using: RUBY_PLATFORM #=> "x64-mingw32" #JRuby #=> "java" RUBY_VERSION #=> "2.5.3" # Next I chose a Ruby method to show how I think Method#arity works; # I decided to use String#chomp. # First I show how it behaves with different numbers of arguments, # then I show the (actual - and expected!) result of s.method(:chomp).arity, # and also of s.method(:length).arity because I know String#length has arity 0. # # I also show that to get the arity of a method of an object of a class # we need to call method(:method_name).arity on the object, not the class. # I did that because as well as Dir objects having an instance method "entries" # the Dir class itself has a singleton method also called "entries", # and I wanted to ensure I knew which method I was getting the arity for. 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 # I had found that in JRuby 2.5.3 Dir.entries could have an optional argument # of the string Encoding to be used, but that this was not the case # for my Ruby 2.5.3. So I set up an Encoding object. Encoding.find("filesystem") #=> #<Encoding:Windows-1252> enc = Encoding::ASCII_8BIT #=> #<Encoding:ASCII-8BIT> # Now I call both the instance method "entries" of Dir objects, # and the singleton method "entries" of the Dir class, # showing the results of different combinations of arguments, # and the results of finding the arity of those "entries" methods. # These also show where the behaviour of Ruby and JRuby differ. 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 d.method(:entries).arity #JRuby #=> 0 # implying n = 0 so must have 0 arguments Dir.entries(".") #=> [ array of files in "." ] Dir.entries(".", enc) #=> wrong number of arguments (given 2, expected 1) #JRuby #=> [ array of files in "." ] Dir.entries(".", enc, 3) #=> wrong number of arguments (given 3, expected 1) #JRuby #=> wrong number of arguments (given 3, expected 1..2) Dir.entries() #=> wrong number of arguments (given 0, expected 1) #JRuby #=> wrong number of arguments (given 0, expected 1..2) d.entries() #=> [ array of files in "." ] d.entries(enc) #=> wrong number of arguments (given 1, expected 0) Unsubscribe: <mailto:[email protected]?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>