私が持っているjsonから値を取得し、それを別のファイルに入れて他の値も同じものにしたいと思っています。これは私のコードです:なぜ "write":クローズドストリーム(IOError) "
somefile = File.open("employee_info.txt", "w")
File.open("employee_api_info.txt") do |file|
file.each_line do |line|
url = URI(line)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
request['Accept'] = 'application/vnd.pagerduty+json;version=2'
request['Authorization'] = "Token token=#{token.chomp}"
response = http.request(request)
# puts response.body
data=response.body
jdoc = JSON.parse(data)
somefile.puts "Employee Name: " + jdoc["user"]["name"].gsub(/\w+/, &:capitalize).gsub(/[.]/, ' ')
somefile.puts "Employee Email: " + jdoc["user"]["email"]
somefile.puts "Time Zone: " + jdoc["user"]["time_zone"]
somefile.close
anotherfile = File.open("employee_phone_api.txt", "w+")
jdoc.fetch("user").fetch("contact_methods").each do |contact|
anotherfile.puts contact["self"]
anotherfile.close
end
end
end
私はそれが常に返さ端子を介してそれを実行すると...
`write': closed stream (IOError)
from PagerDutyOncall.rb:93:in `puts'
from PagerDutyOncall.rb:93:in `block (2 levels) in <main>'
from PagerDutyOncall.rb:92:in `each'
from PagerDutyOncall.rb:92:in `block in <main>'
from PagerDutyOncall.rb:69:in `open'
from PagerDutyOncall.rb:69:in `<main>'
誰かが私を助けてくださいことはできますか?
ここに93行目はありますか? – tadman
'File.open(...)do | f |'パターンはここでのみ使用してください。ブロックスタイルと非ブロックスタイルを混在させてマッチングさせると、何が起こっているのかがかなり混乱します。 – tadman
anotherfile.puts contact ["self"]は91 –