我寫了一個快速的小應用程序,它使用一些關鍵字獲取代碼的基本文件,替換關鍵字的文件,並輸出一個替換關鍵字的新文件。Ruby 1.9 Array.to_s的行爲有所不同?
當我使用Ruby 1.8時,我的輸出看起來很好。現在,當使用Ruby 1.9時,我替換的代碼中包含換行符而不是換行符。
例如,我看到:代替
["\r\nDim RunningNormal_1 As Boolean", "\r\nDim RunningNormal_2 As Boolean", "\r\nDim RunningNormal_3 As Boolean"]
:
Dim RunningNormal_1 As Boolean
Dim RunningNormal_2 As Boolean
Dim RunningNormal_3 As Boolean
我使用替換的散列{ 「KEYWORD」=> [ 「1」, 「2」, 「3」]}和被替換行的數組。
我用這個塊完成替換:
resultingLibs.each do |x|
libraryString.sub!(/(<REPEAT>(.*?)<\/REPEAT>)/im) do |match|
x.each do |individual|
individual.to_s
end
end
end
#for each resulting group of the repeatable pattern,
#
#Write out the resulting libs to a combined string
我的直覺是,我打印出數組,而不是數組中的字符串。有關修復的任何建議。當我使用puts調試並打印替換的字符串時,輸出看起來正確。當我使用to_s方法(這是我的應用程序將輸出寫入輸出文件的方式)時,我的輸出看起來不正確。
修復會很好,但我真正想知道的是在Ruby 1.8和1.9之間導致此行爲的變化。 Ruby 1.9中to_s方法有所變化嗎?
*我在Ruby中
示例代碼將是很好 – orip 2010-10-18 15:00:45
我添加的源代碼,我希望是相關的。 – jrsconfitto 2010-10-18 15:32:00
+1。我一直在使用Ruby 1.8和1.9,但從來不知道這種差異! – 2010-10-18 22:19:28