简体   繁体   中英

Ruby printing PS after reading file

I was wondering what is happening with my output. When I run this code it will work fine, I pass in my file name (ex15_sample.txt) and then it will print it out. After it is done printing out the file it prints out two additional characters "PS". Any idea on why this is?

I'm running Ruby 2.0.0p576 (x64) on a windows 8 64 bit machine.

Here is my code:

# 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()

EDIT: As a side note, if I open the file, read and print it using irb I do not get the extra characters. Only if I use the command ruby ex15.rb

PS is the PowerShell prompt. It gets printed by PowerShell after every command. This has absolutely nothing whatsoever to do with Ruby. Try running dir and it will also print PS.

The print method does not append a newline character to the end of your string. Change print txt.read to puts txt.read . You will see the PS power shell prompt on the following line rather at the end of the printed string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM