【问题标题】:Exclude files from FileList on Ruby从 Ruby 上的 FileList 中排除文件
【发布时间】:2011-11-29 03:17:58
【问题描述】:

我有一个带有以下 SRC = FileList['md/*.md'] 的 rakefile,但我想排除一些文件

我试过了

SRC = FileList['md/*.md'].exclude("md/header.md")
SRC = FileList['md/*.md'].exclude(/header/)
SRC = FileList['md/*.md'].exclude(/header.md$/)

但它不起作用,总是列出我所有的文件

一个例子:

代替我的 rakefile.rb,我有一个目录 md,其中包含以下内容:

rakefile.rb
md/
  index.md
  example.md
  header.md

我想列出所有没有 header.md 的内容

【问题讨论】:

  • 您要排除哪些文件?在exclude 之前,您的FileList 是什么样的? SRC 在exclude 之后是什么样子的? “它不起作用”是不够的信息。

标签: ruby rake rakefile


【解决方案1】:

这对我来说很好用:

p FileList['md/*.md']
#=> ["md/example.md", "md/header.md", "md/index.md"]

p FileList['md/*.md'].exclude("md/header.md")
#=> ["md/example.md", "md/index.md"]

p FileList['md/*.md'].exclude(/header/)
#=> ["md/example.md", "md/index.md"]

p FileList['md/*.md'].exclude(/header.md$/)
#=> ["md/example.md", "md/index.md"]

【讨论】:

【解决方案2】:

不知道发生了什么。也许是您使用的 Rake 和/或 ruby​​ 版本?

我已经使用 ruby​​-1.8.7(rake 0.8.7 和 rake 0.9.2.2)和 ruby​​-1.9.2(rake 0.8.7 和 rake 0.9.2.2)测试了您的 sn-ps。所有四种配置都给出了预期的输出。

代码:

puts "no exculde:"
puts FileList['md/*.md']
puts '---'
puts "exclude('md/header.md'):"
puts FileList['md/*.md'].exclude('md/header.md')
puts '----'
puts "exclude(/header/):"
puts FileList['md/*.md'].exclude(/header/)
puts '----'
puts "exclude(/header.md$/):"
puts FileList['md/*.md'].exclude(/header.md$/)

输出:

no exculde:
md/example.md
md/header.md
md/index.md
---
exclude('md/header.md'):
md/example.md
md/index.md
----
exclude(/header/):
md/example.md
md/index.md
----
exclude(/header.md$/):
md/example.md
md/index.md

【讨论】:

  • ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux] 和 rake version 0.9.2.2 在 ubuntu 11.10 上
猜你喜欢
  • 2016-09-29
  • 2011-03-09
  • 2020-12-22
  • 2013-06-01
  • 1970-01-01
  • 2013-02-14
  • 2012-02-12
  • 2017-12-29
相关资源
最近更新 更多