为什么转储到yaml文件后负值变为正值?
Why negative value becomes positive after dumping into yaml file?
我有一个简单的 sinatra 应用程序使用 yaml 文件来处理数据。其中一项功能是 User
可以投票或否决 Question
。投票功能很好,但是我在实现否决功能时遇到了一些奇怪的事情。
简单地说:
- 当一个问题的当前
votes_count
为正数(>= 1
)时,数字会正确减少
- 但是当一个问题的当前
votes_count
为零或负数时,数字会在data
散列中成功减少,但在转储data
散列到yaml文件中后,负数变为阳性。
这是 Question
的 yaml 文件:
'1': !ruby/hash:Sinatra::IndifferentHash
title: " Best way to require all files from a directory in ruby?"
description: What's the best way to require all files from a directory in ruby ?
user_id: '3'
votes_count: 0
# other user information
这是与否决功能相关的路由处理程序:
post "/questions/:id/veto" do
check_vote_validity_for_question(params[:id])
@question = Question.find_by(:id, params[:id])
@question.votes_count = (@question.votes_count.to_i - 1)
Question.update(params[:id], votes_count: @question.votes_count )
# omit user related code
end
这是update
方法:
def self.update(id, attrs)
data = load_data_of(data_name)
# binding.pry
obj_info = data[id]
attrs.each do |k, v|
v = v.to_s if v.is_a?(Array)
obj_info[k] = v
end
# binding.pry
File.open(File.join(data_path, "#{data_name.to_s}.yaml"), "w+") do |f|
f.write(Psych.dump(data).delete("---"))
end
end
如果我在更新 data
哈希之前和之后暂停 update
方法中的程序,它表明 votes_count
的值设置正确。
之前:
[1] pry(Question)> data
=> {"1"=>
{"title"=>" Best way to require all files from a directory in ruby?",
"description"=>"What's the best way to require all files from a directory in ruby ?",
"user_id"=>"3",
"votes_count"=>0},
之后:
[1] pry(Question)> data
=> {"1"=>
{"title"=>" Best way to require all files from a directory in ruby?",
"description"=>"What's the best way to require all files from a directory in ruby ?",
"user_id"=>"3",
"votes_count"=>-1},
data
哈希中的键 "votes_count"
更新后的值为 -1
,但在我将 data
哈希转储到 yaml 文件后, [ yaml文件中用户的=27=]变成了1
。而如果hash中的值为-2
,那么在yaml文件中就会变成2
我尝试制作一个在 irb 中具有负值的散列,然后将其转储到 yaml 文件中,一切正常。我不知道发生了什么事。有人可以帮助我吗?
看起来问题在行
f.write(Psych.dump(data).delete("---"))
你删除-
.
例如
"-1".delete("---") #=> "1"
我有一个简单的 sinatra 应用程序使用 yaml 文件来处理数据。其中一项功能是 User
可以投票或否决 Question
。投票功能很好,但是我在实现否决功能时遇到了一些奇怪的事情。
简单地说:
- 当一个问题的当前
votes_count
为正数(>= 1
)时,数字会正确减少 - 但是当一个问题的当前
votes_count
为零或负数时,数字会在data
散列中成功减少,但在转储data
散列到yaml文件中后,负数变为阳性。
这是 Question
的 yaml 文件:
'1': !ruby/hash:Sinatra::IndifferentHash
title: " Best way to require all files from a directory in ruby?"
description: What's the best way to require all files from a directory in ruby ?
user_id: '3'
votes_count: 0
# other user information
这是与否决功能相关的路由处理程序:
post "/questions/:id/veto" do
check_vote_validity_for_question(params[:id])
@question = Question.find_by(:id, params[:id])
@question.votes_count = (@question.votes_count.to_i - 1)
Question.update(params[:id], votes_count: @question.votes_count )
# omit user related code
end
这是update
方法:
def self.update(id, attrs)
data = load_data_of(data_name)
# binding.pry
obj_info = data[id]
attrs.each do |k, v|
v = v.to_s if v.is_a?(Array)
obj_info[k] = v
end
# binding.pry
File.open(File.join(data_path, "#{data_name.to_s}.yaml"), "w+") do |f|
f.write(Psych.dump(data).delete("---"))
end
end
如果我在更新 data
哈希之前和之后暂停 update
方法中的程序,它表明 votes_count
的值设置正确。
之前:
[1] pry(Question)> data
=> {"1"=>
{"title"=>" Best way to require all files from a directory in ruby?",
"description"=>"What's the best way to require all files from a directory in ruby ?",
"user_id"=>"3",
"votes_count"=>0},
之后:
[1] pry(Question)> data
=> {"1"=>
{"title"=>" Best way to require all files from a directory in ruby?",
"description"=>"What's the best way to require all files from a directory in ruby ?",
"user_id"=>"3",
"votes_count"=>-1},
data
哈希中的键 "votes_count"
更新后的值为 -1
,但在我将 data
哈希转储到 yaml 文件后, [ yaml文件中用户的=27=]变成了1
。而如果hash中的值为-2
,那么在yaml文件中就会变成2
我尝试制作一个在 irb 中具有负值的散列,然后将其转储到 yaml 文件中,一切正常。我不知道发生了什么事。有人可以帮助我吗?
看起来问题在行
f.write(Psych.dump(data).delete("---"))
你删除-
.
例如
"-1".delete("---") #=> "1"