2014-11-05 33 views
0

我想知道我的輸出是怎麼回事。當我運行這段代碼時,它會正常工作,我傳入我的文件名(ex15_sample.txt),然後將它打印出來。打印完文件後,它會打印出兩個附加字符「PS」。任何想法,爲什麼這是?閱讀文件後紅寶石打印PS

我在Windows 8 64位機器上運行Ruby 2.0.0p576(x64)。

這裏是我的代碼:

# Prompts the user for input on the filename 
print "What is the name of the file you would like printed? " 

# Takes a command line argument and assigns it to the variable 
filename = gets.chomp 

# Declares another variable and initializes it by opening the file stored in the variable filename 
txt = open(filename) 

# Prints out a string with an interpolated string 
puts "Here's your file #{filename}:" 
# Prints out the file stored in the variable txt 
print txt.read 
# Closes the file 
txt.close() 

編輯:作爲一個方面說明,如果我打開文件,讀取和使用irb我沒有得到多餘的字符打印。只有當我使用命令ruby ex15.rb

+1

你是否在PowerShell提示符下運行這個? – ptierno 2014-11-05 20:42:05

回答

2

PS是PowerShell提示符。每次執行命令後都會由PowerShell打印。這與Ruby完全沒有任何關係。嘗試運行dir,它也會打印PS。

0

print方法不會將一個換行符附加到字符串的末尾。將print txt.read更改爲puts txt.read。您將在下面的行中看到PS power shell提示符,而不是在打印的字符串的末尾。