2011-02-18 47 views

回答

26

由於可能會從行中單獨寫入新行,因此存在競爭條件。您可以在多線程應用程序中使用看跌期權看到這種噪音:

thread 0thread 1 
thread 0thread 2 
thread 1 
thread 0thread 3 
thread 2 
thread 1 

相反,使用print或printf

print "thread #{i}" + "\n" 
print "thread #{i}\n" 
printf "thread %d\n", i 

或者,因爲你要寫入STDERR:

$stderr.print "thread #{i}\n" 

這是Ruby中的錯誤嗎?不是如果評論是作爲標準。這裏是從MRI 1.8.7到2.2.2的IO.puts的定義:

/* 
* call-seq: 
*  ios.puts(obj, ...) => nil 
* 
* Writes the given objects to <em>ios</em> as with 
* <code>IO#print</code>. Writes a record separator (typically a 
* newline) after any that do not already end with a newline sequence. 
* If called with an array argument, writes each element on a new line. 
* If called without arguments, outputs a single record separator. 
* 
*  $stdout.puts("this", "is", "a", "test") 
* 
* <em>produces:</em> 
* 
*  this 
*  is 
*  a 
*  test 
*/ 
+0

哇,我剛剛在我的一些日誌文件中與此生活在一起。看起來像一個簡單的修復。 – 2011-02-18 18:22:03