怎么做一个内置于ruby目录中的目录列表?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (11)

有没有一个更清洁的方法来做这件事?

ree> Pathname.new('/path/to').children.select{|e| e.directory?}.map{|d| d.basename.to_s}
 => ["test1", "test2"]

理想情况下,我想避免calldirectory?

提问于
用户回答回答于

你可以使用

Dir['app/*/']
# => ["app/controllers/", "app/helpers/", "app/metal/", "app/models/", "app/sweepers/", "app/views/"

Dir['app/*/'].map { |a| File.basename(a) }
# => ["controllers", "helpers", "metal", "models", "sweepers", "views"]

如果使用Ruby>=1.8.7,也可以:

Pathname.glob('app/*/').map(&:basename)
# you can skip .to_s unless you don't need to work with strings
# remember you can always use a pathname as string for the most part of Ruby functions
# or interpolate the value
用户回答回答于
Pathname.glob("/path/to/*/").map { |i| i.basename.to_s }

扫码关注云+社区