我有一些处理Gemfile的Ruby代码。它添加了一些推荐的宝石,并删除了其他的宝石。Gemfile的一个部分如下所示:
group :development, :test do
# The gem version is a recommended starting place; upgrade if needed.
gem 'pry-rails', '~> 0.3.4'
# Enhance pry with byebug (which gives more debugger commands and other goodies).
# The gem version is a recommended starting place; upgrade if needed.
gem 'pry-byebug', '~> 3.4.0'
# Use rspec for testing
gem 'rspec-rails', '~> 3.5.1'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: mri
end我使用以下两行删除块的最后两行( gem 'byebug ...行和上面的注释)。
gsub_file(full_app_gemfile_path, /^\s*gem\s*("|')byebug.*$/, "", verbose: false)
gsub_file(full_app_gemfile_path, /^\s*#.*Call.*("|')byebug("|').*$/, "", verbose: false)gsub_file是Thor gem提供的一种方法。移除工作,但我在Gemfile中得到了以下代码
group :development, :test do
# The gem version is a recommended starting place; upgrade if needed.
gem 'pry-rails', '~> 0.3.4'
# Enhance pry with byebug (which gives more debugger commands and other goodies).
# The gem version is a recommended starting place; upgrade if needed.
gem 'pry-byebug', '~> 3.4.0'
# Use rspec for testing
gem 'rspec-rails', '~> 3.5.1'
end为什么在group :development, :test do之后插入额外的空行?离线被移开的地方还很远。它可能是Thor宝石中的一个bug,但我想知道它是否是regex问题。
更新
我刚刚尝试使用原始的ruby (以消除潜在的Thor问题)。我创建了一个助手方法
def my_gsub(path, regex, str)
text = File.read(path)
rep = text.gsub(regex, str)
File.open(path, "w") {|file| file.puts rep}
end当我将调用gsub_file的两行更改为调用my_gsub时,现在在group :development, :test do之后得到两个空行。
发布于 2017-04-10 16:03:05
对于您的方法my_gsub,您要替换行的内容而不替换换行符。为了使它也移除换行符,您可以将regex更改为:
gsub_file(full_app_gemfile_path, /^\s*gem\s*("|')byebug.*$\n/, "")
gsub_file(full_app_gemfile_path, /^\s*#.*Call.*("|')byebug("|').*$\n/, "")https://stackoverflow.com/questions/43327165
复制相似问题