我希望目录中除隐藏文件(名称以点开头的文件)之外的所有文件:
@files = Pathname.new('.').children.select do |file|
file.basename[0] != '.'
end我不能这样做,因为#basename以"<#Pathname:.envrc>"的形式返回字符串。所以我需要让它成为file.basename =~ '#<Pathname:.',这对我来说似乎很奇怪。
为什么他们在前面加上“路径名”这个词?
发布于 2014-06-10 03:39:45
看看OP的答案,我想它可以写成
@files = Pathname.new('.').children(false).reject do |file|
file.to_s.start_with? '.'
endchildren的Doc说-如果您将with_directory设置为false,那么返回的路径名将只包含文件名。这意味着,我们将获得所有的基本名称作为路径名对象,如Pathname:.git,Pathname:English.rb。现在应用#to_s,我们将拥有'.git','English.rb'。因此,现在我们可以对它使用String#start_with?方法来测试它是否以.开头。
发布于 2014-06-10 03:31:08
正如Arup建议的那样,要列出隐藏文件之外的所有文件,我应该这样做:
@files = Pathname.new('.').children.select do |file|
file.to_s[0] != '.'
endhttps://stackoverflow.com/questions/24127429
复制相似问题