我有一个Hash @answers = params [:Answers] .to_hash;
<%= debug @answers %>
出局
---
"1": "2"
"7": "3"
"6": "4"
"4": "0"
需要将@answers设为如下所示
@ans = {1 => 2, 7 => 3, 6 => 4, 4 => 0}
<%= debug @answers %>
出局
---
1: 2
7: 3
6: 4
4: 0
答案 0 :(得分:11)
黑魔法向导报道:
answers = {"1" => "2", "3" => "4"}
Hash[*answers.to_a.flatten.map(&:to_i)] # => {1=>2, 3=>4}
答案 1 :(得分:3)
这是代码
@ans = {"1" => "2", "7" => "3", "6" => "4", "4" => "0"}
@foo_hash ={} #new_hash
@ans.each_pair{|k,v| @foo_hash.store(k.to_i,v.to_i)}
@foo_hash将为{1 => 2, 7 => 3, 6 => 4, 4 => 0}
然后你可以&lt;%=调试@foo_hash%&gt;获取YAML格式的输出