我有一些處理Gemfile的Ruby代碼。它增加了一些推薦的寶石,並刪除了其他寶石。有看起來類似下面的Gemfile中的一部分:爲什麼在使用Thor的Ruby文件中使用gsub後會出現額外的空白行?
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
寶石的方法。拆卸工程,但我最終在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,但我想知道它是否是正則表達式問題。
更新
我只是用原料紅寶石GSUB試圖(以消除潛在的雷神問題)。我創建了一個輔助方法,
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
後得到兩個空行。
看起來像一個托爾bug。你能檢查兩個'gsub'中哪一個添加了這一行嗎? – ndn
我剛更新了文字。我創建了一個輔助方法來直接調用Ruby'gsub'命令,這樣Thor就可以從故障排除中消除。現在我得到兩個空行。 – CodeSmith