我正在根据哈希值分配一组值.
我必须根据传入的哈希键的值为另一个哈希键分配值.
传入哈希中有超过10个键(因此超过10个案例).
我想最小化代码.是否有任何替代方法可以缩短代码.
@hash1.each do |h1|
case h1.mapped_field
when 'value1'
@hash2[h1.field_id] = value_1
when 'value2'
@hash2[h1.field_id] = value_2
when 'value3'
@hash2[h1.field_id] = value_3
when 'value4'
@hash2[h1.field_id] = value_4
when 'value5'
@hash2[h1.field_id] = value_5
end
end
Run Code Online (Sandbox Code Playgroud)