Ruby: reduce duplication while initialize hash

Posted by user612308 on Stack Overflow See other posts from Stack Overflow or by user612308
Published on 2011-02-11T01:03:45Z Indexed on 2011/02/11 15:25 UTC
Read the original article Hit count: 144

Filed under:
array = [0, 0.3, 0.4, 0.2, 0.6]

hash = {
   "key1" => array[0..2],
   "key2" => array[0..3],
   "key3" => array,
   "key4" => array,
   "key5" => array,
   "key6" => array,   
   "key7" => array
}

Is there a way I can remove the duplication by doing something like

hash = {
   "key1" => array[0..2],
   "key2" => array[0..3],
    %(key3, key4, key5, key6, key7).each {|ele| ele => array}
}

© Stack Overflow or respective owner

Related posts about ruby