阅读文件后Ruby打印PS

时间:2014-11-05 20:35:48

标签: ruby file-io printing

我想知道我的输出发生了什么。当我运行此代码它将正常工作,我传入我的文件名(ex15_sample.txt),然后它将打印出来。打印完文件后,打印出两个额外的字符" 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

2 个答案:

答案 0 :(得分:2)

PS是PowerShell提示符。每次命令后,PowerShell都会打印出来。这与Ruby完全没有任何关系。尝试运行dir,它也会打印PS。

答案 1 :(得分:0)

print方法不会在字符串末尾附加换行符。将print txt.read更改为puts txt.read。您将在以下行中看到PS电源shell提示符,而不是在打印字符串的末尾。