FakeProfilePictures::Photo.all_large_names_2x
(定义如下)返回绝对路径名数组,但是当我执行 Dir["picture_*@2x.*"]
从 irb
中的正确目录,我只得到基本名称(我想要的)。获取基本名称的最佳方法是什么?我知道我可以通过添加 .map { |f| 来做到这一点File.basename(f) }
如评论中所示,但是否有更简单的/better/faster/stronger怎么办?
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
最佳答案
你可以做到
Dir.chdir(DIR) do
Dir["picture_*@2x.*"]
end
block 后,原来的目录被恢复。
关于ruby - 如何让 Ruby Dir#glob 返回基本名称,而不是绝对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5810782/