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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With