Thursday, February 28, 2008

square brackets [] class method of Hash

[] class method of Hash converts an array into a hash.


irb(main):001:0> Hash["name","subbu","profession","programmer"]
=> {"name"=>"subbu", "profession"=>"programmer"}
irb(main):002:0> my_array = %w{ a b c d }
=> ["a", "b", "c", "d"]
irb(main):003:0> Hash[*my_array]
=> {"a"=>"b", "c"=>"d"}

No comments: