在 ruby 中以内置方式列出目录中的目录
有没有更干净的内置方法来做到这一点?
ree> Pathname.new('/path/to').children.select{|e| e.directory?}.map{|d| d.basename.to_s}
=> ["test1", "test2"]
理想情况下,我想避免 directory?
调用
Is there a cleaner built-in way to do this?
ree> Pathname.new('/path/to').children.select{|e| e.directory?}.map{|d| d.basename.to_s}
=> ["test1", "test2"]
Ideally I would like to avoid the directory?
call
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从Chandra的答案开始,根据您是否需要完整路径,您可以使用
如果您使用Ruby >= 1.8.7,Chandra的答案也可以重写为
Starting from Chandra's answer, depending on whether you need or not the full path, you can use
If you use Ruby >= 1.8.7, Chandra's answer can also be rewritten as