Ruby tip 0001: Initialize hash from string or array
Problem:
Convert 'ABCD' to {"A"=>0, "B"=>0, "C"=>0, "D"=>0}
Using
inject
'ABCD'.chars.inject({}) { |memo, v| memo[v] = 0; memo }
Using
Hash
Hash['ABCD'.chars.map { |v| [v, 0] }]
Convert 'ABCD' to {"A"=>0, "B"=>0, "C"=>0, "D"=>0}
Using inject
'ABCD'.chars.inject({}) { |memo, v| memo[v] = 0; memo }
Using Hash
Hash['ABCD'.chars.map { |v| [v, 0] }]