Actions
Bug #15576
closedWrong error message from some class methods of `Dir`
    Bug #15576:
    Wrong error message from some class methods of `Dir`
  
Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-linux]
Description
When a error is raised in some class methods of Dir, the backtrace is as if the user called Dir.open.
$ ruby -e 'Dir.foreach {}'
Traceback (most recent call last):
	2: from -e:1:in `<main>'
	1: from -e:1:in `foreach'
-e:1:in `open': wrong number of arguments (given 0, expected 1) (ArgumentError)
$ ruby -e 'Dir.foreach(nil)'
Traceback (most recent call last):
	2: from -e:1:in `<main>'
	1: from -e:1:in `foreach'
-e:1:in `open': no implicit conversion of nil into String (TypeError)
$ ruby -e 'Dir.foreach("not_exist")'
Traceback (most recent call last):
	2: from -e:1:in `<main>'
	1: from -e:1:in `foreach'
-e:1:in `open': No such file or directory @ dir_initialize - not_exist (Errno::ENOENT)
The last line of the error message should be -e:1:in `foreach': ... like IO.foreach
This occurs in following methods:
- Dir.foreach
- Dir.entries
- Dir.each_child
- Dir.children
        
           Updated by nobu (Nobuyoshi Nakada) over 6 years ago
          Updated by nobu (Nobuyoshi Nakada) over 6 years ago
          
          
        
        
      
      It is expected as these methods delegate to Dir.open.
So you can redefine the open method.
$ ruby -e 'def Dir.open(*)raise "boom";end; Dir.foreach("."){}'
Traceback (most recent call last):
	2: from -e:1:in `<main>'
	1: from -e:1:in `foreach'
-e:1:in `open': boom (RuntimeError)
whereas IO.foreach can't.
        
           Updated by nobu (Nobuyoshi Nakada) over 6 years ago
          Updated by nobu (Nobuyoshi Nakada) over 6 years ago
          
          
        
        
      
      I'm not sure this difference between IO and Dir is intentional.
        
           Updated by jeremyevans0 (Jeremy Evans) about 5 years ago
          Updated by jeremyevans0 (Jeremy Evans) about 5 years ago
          
          
        
        
      
      - Status changed from Open to Closed
Actions