Joe Ercolino Joe Ercolino - 1 year ago 74
Ruby Question

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

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?

Answer Source

This seems to be some kind of documentation mess.

The Dir.children method was introduced with Feature #11302 into Ruby and was comitted to trunk. 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 released version:

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download