Ruby でのコーディング方法に関する一連のチュートリアルを行っています。この式を使用して .txt ファイルを読みたい:
filename = ARGV.first
prompt = "> "
txt = File.open(filename)
puts "Here's your file: #{filename}"
puts txt.read()
puts "I'll also ask you to type it again:"
print prompt
file_again = STDIN.gets.chomp()
txt_again = File.open(file_again)
puts txt_again.read()
テキストファイルは次のとおりです。
This is stuff I typed into a file. It is really cool stuff.
Lots and lots of fun to have in here.
テキストファイルの名前はex15_sample.txt
. 上記の式で試してみましたが、何も機能しないようです。ARGV
との両方の使い方がよくわかりませんSTDIN.gets.chomp
。
私は何をすべきか?上記の式を使用してください。これは少しややこしいので、今のところは上記の式を使用してください。