You can get a hash containing the last key in the array (by deleting the last element), and then set the key value:
array.map!(&:to_sym) # make sure keys are symbols
key = array.pop
array.inject(hash, :fetch)[key] = 40
hash # => {:person=>{:age=>40, :name=>"tom"}}
If you do not want to modify the array, you can use .lastand [0...-1]:
keys = array.map(&:to_sym)
key = keys.last
keys[0...-1].inject(hash, :fetch)[key] = 40
source
share