简体   繁体   English

在带有Thor的Ruby中对文件使用gsub后,为什么还有多余的空行?

[英]Why is there an extra blank line after using gsub on file in Ruby with Thor?

I have some Ruby code that processes a Gemfile. 我有一些处理Gemfile的Ruby代码。 It adds some recommended gems and removes other gems. 它添加了一些推荐的宝石,并删除了其他宝石。 There's a section of a Gemfile that looks like the following: 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

I'm using the following two lines to remove the last two lines of the block (the gem 'byebug ... line and the comment above it). 我正在使用以下两行来删除块的最后两行( 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)

The gsub_file is a method provided by the Thor gem. gsub_fileThor宝石提供的一种方法。 The removal works, but I end up with the following code in the Gemfile 删除有效,但我最终在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

Why is that extra blank line being inserted after group :development, :test do ? 为什么在group :development, :test do之后插入多余的空行? It's nowhere near where the lines were removed. 离删除线不远。 It could possibly be a bug in the Thor gem, but I'm wondering if it's regex issue. 它可能是Thor Thor宝石中的错误,但我想知道这是否是正则表达式问题。

Update 更新资料

I just tried using the raw ruby gsub (to eliminate potential Thor issues). 我只是尝试使用原始的红宝石gsub(以消除潜在的Thor问题)。 I created a helper method 我创建了一个辅助方法

def my_gsub(path, regex, str)
  text = File.read(path)
  rep = text.gsub(regex, str)
  File.open(path, "w") {|file| file.puts rep}
end

When I change the two lines that are calling gsub_file to call my_gsub , I now get two blank lines after group :development, :test do . 当我将调用gsub_file的两行更改为调用my_gsub ,在group :development, :test do之后,我现在得到两条空行。

For your method my_gsub you are replacing the content of the line without replacing the linebreak. 对于方法my_gsub您将替换行的内容而不替换换行符。 To make it remove the newline as well you can change your regex to this: 要使其也删除换行符,您可以将正则表达式更改为此:

gsub_file(full_app_gemfile_path, /^\s*gem\s*("|')byebug.*$\n/, "")
gsub_file(full_app_gemfile_path, /^\s*#.*Call.*("|')byebug("|').*$\n/, "")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM