为什么Dir.glob在Rails应用程序模板中返回一个空数组?

时间:2022-10-08 19:50:02

I am implementing a custom Rails app template with a .remove file that lists glob-style patterns of files (e.g. app/**/*.erb) to be removed after bundle install finishes:

我正在实现一个带有.remove文件的自定义Rails应用程序模板,该文件列出了在bundle安装完成后要删除的文件样式(例如app / ** / * .erb):

removals = File.readlines('/path/to/.remove')
cwd = Dir.getwd

after_bundle do
  # Process removals
  removals.each do |pattern|

    puts 'glob:'+cwd+'/'+pattern           # glob:/Users/acobster/starfruit/app/**/*.erb
    puts Dir.glob(cwd+'/'+pattern).count   # 0
    puts `tree #{cwd}/app`                 # all files in the app tree, 
                                           # including several .erb files

    Dir.glob(cwd+'/'+pattern).each do |filepath|
      puts "        removing #{filepath}"
      remove_file(filepath)
    end
  end
end

According to the debug code (see comments above), the files I want to delete (in this case all .erb files under the app/ tree) are there in the tree, but the equivalent of Dir.glob('/Users/acobster/starfruit/app/**/*.erb') is returning an empty array.

根据调试代码(参见上面的注释),我想要删除的文件(在本例中是app / tree下的所有.erb文件)都在树中,但相当于Dir.glob('/ Users / acobster) /starfruit/app/**/*.erb')返回一个空数组。

Weirder yet, when I run Dir.glob('/Users/acobster/starfruit/app/**/*.erb') from irb, it correctly returns the array with the three .erb files present.

更奇怪的是,当我从irb运行Dir.glob('/ Users / acobster / starfruit / app / ** / * .erb')时,它正确返回存在三个.erb文件的数组。

What's going on?

这是怎么回事?

1 个解决方案

#1


1  

Turns out I was just forgetting to strip off newlines from the glob patterns. I changed the first line to

事实证明我只是忘记从glob模式中删除换行符。我改变了第一行

removals = File.readlines('/path/to/.remove').map(&:strip).reject(&:empty?)

#1


1  

Turns out I was just forgetting to strip off newlines from the glob patterns. I changed the first line to

事实证明我只是忘记从glob模式中删除换行符。我改变了第一行

removals = File.readlines('/path/to/.remove').map(&:strip).reject(&:empty?)