Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 为什么它会说`写';:封闭流(IOError)“;_Ruby_Io - Fatal编程技术网

Ruby 为什么它会说`写';:封闭流(IOError)“;

Ruby 为什么它会说`写';:封闭流(IOError)“;,ruby,io,Ruby,Io,我希望能够从我拥有的json中获取一个值,并将其放在一个不同的文件中,然后其他值也将放在另一个文件中。这是我的代码: 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) htt

我希望能够从我拥有的json中获取一个值,并将其放在一个不同的文件中,然后其他值也将放在另一个文件中。这是我的代码:

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>'
`write':关闭流(IOError)
来自PagerDutyOncall.rb:93:in'puts'
来自PagerDutyOncall.rb:93:in'block(2层)in'
来自PagerDutyOncall.rb:92:in'each'
来自PagerDutyOncall.rb:92:in'block in'
来自PagerDutyOncall.rb:69:in'open'
来自PagerDutyOncall.rb:69:in`'

有人能帮我吗?

由于缩进无政府状态和使用手动打开/关闭,我认为是您造成了这个问题。以下是正确缩进的代码:

anotherfile = File.open("employee_phone_api.txt", "w+")
jdoc.fetch("user").fetch("contact_methods").each do |contact|
  anotherfile.puts contact["self"]
  anotherfile.close
end
请注意,
另一个文件.close
位于循环内。这是你的问题。通过正确嵌套对象来修复它:

File.open("employee_phone_api.txt", "w+") do |af|
  jdoc.fetch("user").fetch("contact_methods").each do |contact|
    af.puts contact["self"]
  end
end

如果使用
ruby-w
运行此程序,您应该会收到有关不一致缩进的警告,这将有助于从一开始就避免此类问题。请记住:干净的代码会使肮脏的错误更加明显。

这里哪一行是93?您应该使用
文件。在这里只打开(…)do | f |
模式。块样式和非块样式之间的混合和匹配给正在发生的事情增加了相当大的混乱。anotherfile.puts contact[“self”]是91谢谢你,它可以工作!不过我还有一个问题?因此,它只为文本文件employee_api_info.txt的一行打印self的值。我如何循环它,以便获得该文本中所有行(api)的值。如果你需要澄清,请告诉我。别客气!我得到了它。我不得不把w+换成a。对不起,愚蠢的错误。如果你修复了它并学到了一些东西,那就没那么愚蠢了。很高兴我能帮忙。