gpt4 book ai didi

ruby-on-rails - File.read 返回 nil

转载 作者:数据小太阳 更新时间:2023-10-29 08:46:56 25 4
gpt4 key购买 nike

我在 ruby​​ 中创建了一个文件并在其中保存了一些哈希值。当我阅读文件时,我得到了零。这是代码:

my_hash = Hash.new
file = File.open("my_file.json", "w")

my_hash["test_key"] = "0.1"
file.write(my_hash.to_json)

file_read = File.read("my_file.json")
p file_read // This prints nil

当我打开文件时,我看到 {"test_key":"0.1"}

我是不是漏掉了什么?

最佳答案

您在写入文件后没有关闭文件,因此很可能是在您尝试从文件中读取时操作系统尚未将挂起的 IO 刷新到文件中。就操作系统而言,该文件尚不存在。

关于ruby-on-rails - File.read 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25122036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com