Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby 2.4.1 Dir.children( dirname ) returns "undefined method `children' for Dir:Class"

Tags:

ruby

I'm new to Ruby and trying to learn it. I'm using the latest Ruby version (2.4.1) and the interactive Ruby Shell.

I've come across the children method in the Dir class. I've tried the example from the documentation:

Dir.children("testdir")   #=> ["config.h", "main.rb"]

but it doesn't seem to work, because I get the following message:

undefined method `children' for Dir:Class

What am I missing?

like image 202
Joe Ercolino Avatar asked Aug 16 '17 17:08

Joe Ercolino


1 Answers

This seems to be some kind of documentation mess.

The Dir.children method was introduced with Feature #11302 into Ruby and was committed to trunk and eventually released with Ruby 2.5.0. However, it appears that the patch adding this method was not actually backported to Ruby 2.4 since dir.c of Ruby 2.4.1 doesn't mention the method. It's not immediately clear why the documentation for this method turned up at http://ruby-doc.org/

In any case, it appears you are yet out of luck with this method. You can however use the following equivalent code with your Ruby version:

Dir.entries('testdir') - [".", ".."]

It will return the exact same values as Dir.children('testdir') would in Ruby 2.5 and newer.

like image 181
Holger Just Avatar answered Oct 19 '22 19:10

Holger Just