从{"示例" => "示例"}到{:sample => "样品"}

时间:2015-11-07 15:38:10

标签: ruby-on-rails ruby

In condition,
COLUMN = [:id, :tag_list, :price, :url, :Perweight, :Totalweight, :memo, :created_at, :updated_at]

row = {"id"=>4, "tag_list"=>"peanuts", "price"=>100, "Totalweight"=>390, "Perweight"=>nil, "url"=>nil, "memo"=>nil, nil=>nil}

from these two conditions, I want to make above Hash Object.

{:id=>4, :tag_list=>"peanuts", :price=>100, :Totalweight"=>390, :Perweight=>nil, :url=>nil, memo=>nil}

我试过,像这样...... 起初,我做空哈希,

new = Hash[COLUMN.zip([])] 
p new
--->
{:id=>nil, :tag_list=>nil, :price=>nil, :url=>nil, :Perweight=>nil, :Totalweight=>nil, :memo=>nil, :created_at=>nil, :updated_at=>nil}

然后,我不知道该怎么做, 请给我建议吗?

1 个答案:

答案 0 :(得分:4)

您可以使用Symbolize Keys

myval = myval & (32'('1) <<savebit_loc);

或破坏性地

row.symbolize_keys