Monday, May 23, 2011

splat

Found a nifty way to convert arrays to hashes and vise-versa,
ruby-1.9.2-p0 :037 > data = [["a", 1], ["b", 2], ["c", 3]]
 => [["a", 1], ["b", 2], ["c", 3]] 
ruby-1.9.2-p0 :039 > h = Hash[*data.flatten]
 => {"a"=>1, "b"=>2} 
and back
ruby-1.9.2-p0 :048 > ar = *h
 => [["a", 1], ["b", 2]] 

No comments:

Post a Comment