我試圖從運行的Zed Shaw的這個代碼瞭解紅寶石堅硬方式行使15:Ruby的File.open不能轉換成零字符串(類型錯誤)
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()
我每次運行它,我得到一條錯誤消息說:
ex15.rb:4:in `initialize': can't convert nil into String (TypeError)
from ex15.rb:4:in `open'
from ex15.rb:4
這是怎麼回事?我無法獲得此代碼的工作!
當你執行它時,你是否像這樣'ruby ex15.rb sample_text.txt'執行它? –