FakeProfilePictures :: Photo.all_large_names_2x(在下面定义)返回一个绝对路径名的数组,但是当我从irb中的正确目录执行Dir [“picture _ * @ 2x.*”]时,我只获得了基本名称(我想要的) .获取基本名称的最佳方法是什么?我知道我可以通过添加.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
在块之后,原始目录被恢复.