Ruby tip 0001: Initialize hash from string or array

Problem:

Convert 'ABCD' to {"A"=>0, "B"=>0, "C"=>0, "D"=>0}

  1. Using inject

     'ABCD'.chars.inject({}) { |memo, v| memo[v] = 0; memo  }
    
  2. Using Hash

     Hash['ABCD'.chars.map { |v| [v, 0] }]