Ruby on rails Ruby/RoR-字符串哈希到整数哈希

Ruby on rails Ruby/RoR-字符串哈希到整数哈希,ruby-on-rails,ruby,hash,Ruby On Rails,Ruby,Hash,我有一个Hash@answers=params[:answers] 输出 --- "1": "2" "7": "3" "6": "4" "4": "0" 需要对下面这样的问题做出@answers @ans={1=>2,7=>3,6=>4,4=>0} 输出 --- 1: 2 7: 3 6: 4 4: 0 这是密码 @ans = {"1" => "2", "7" => "3", "6" => "4", "4" => "0"} @foo_hash ={} #new_ha

我有一个Hash@answers=params[:answers]

输出

--- 
"1": "2"
"7": "3"
"6": "4"
"4": "0"
需要对下面这样的问题做出@answers

@ans={1=>2,7=>3,6=>4,4=>0}
输出

--- 
1: 2
7: 3
6: 4
4: 0
这是密码

@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_散列将是
{1=>2,7=>3,6=>4,4=>0}


然后,您可以通过以下方式获得YAML格式的输出

黑魔法向导报告:

answers = {"1" => "2", "3" => "4"}
Hash[*answers.to_a.flatten.map(&:to_i)] # => {1=>2, 3=>4}

你为什么要那样?这是一个调试,所以它就像你做的
p
no
put
p:
debug
,在Rails视图中,将其参数格式化为yaml。在ruby 1.8.7和1.9.2上进行双重测试。确保“*”存在且答案不为空