読みたいテキストファイル「test.txt」があります。いくつかの行があります:
line1
line2
trim(1255, 158597)
#712, 272, 4, 102
次のコードを使用しています。
itrimcmd = ""
secondline = ""
File.open("test.txt").each_line { |line|
puts "[8]... #{line}"
if line =~ /^trim/ then itrimcmd = line end
if line =~ /^#/ then secondline = line end
}
puts "itrimcmd: #{itrimcmd}"
puts "secondline: #{secondline}"
私のコードはこのファイルでは動作しません。出力:
#712, 272, 4, 102)
itrimcmd:
secondline:
まったく同じ内容の 2 番目のファイルを再入力すると、今度は正しい結果が得られます。
line1
line2
trim(1255, 158597)
#712, 272, 4, 102
2 つのテキスト ファイルに違いはありません。正しい出力:
[2]... line1
[2]... line2
[2]... trim(1255, 158597)
[2]... #712, 272, 4, 102
itrimcmd: trim(1255, 158597)
secondline: #712, 272, 4, 102
Windows 7 で Ruby 1.9.3 を使用しています。