8

FakeProfilePictures::Photo.all_large_names_2x(定义如下)返回一个绝对路径名数组,但是当我Dir["picture_*@2x.*"]从正确的目录中执行时irb,我只得到基本名称(我想要的)。获得基本名称的最佳方法是什么?我知道我可以通过添加.map { |f| File.basename(f) }如评论中所示来做到这一点,但有没有更简单/更好/更快/更强的方法?

module FakeProfilePictures
  class Photo
    DIR = File.expand_path(File.join(File.dirname(__FILE__), "photos"))

    # ...

    def self.all_large_names_2x
      @@all_large_names_2x ||= Dir[File.join(DIR, "picture_*@2x.*")] # .map { |f| File.basename(f) }
    end
  end
end
4

2 回答 2

21

你可以做

Dir.chdir(DIR) do
  Dir["picture_*@2x.*"]
end

块之后,恢复原始目录。

于 2011-04-27T21:45:06.403 回答
3

你可以在 globbing 之前进入,但我会chdir通过.DIRbasename

于 2011-04-27T21:41:39.120 回答