You can use each_with_object to iterate over "peaple", and assign to a new hash the current element using as a key the prefix person plus the index of the current element (person).
peaple
.each_with_object({})
.with_index(1) do |(person, hash), index|
hash["person#{index}"] = person
end
# {"person1"=>{:name=>"Sam", :year=>"21"},
# "person2"=>{:name=>"cole", :partition=>"20"},
# "person3"=>{:name=>"bart", :year=>"21"}}
Another version just out of curiosity would be to create an array of strings with the same length of "peaple", having as values the prefix "person" plus its index plus 1. Zipping that with the current value and invoking to_h on it gives the same result:
Array.new(peaple.length) { |i| "person#{i + 1}" }.zip(peaple).to_h
If the idea is getting who's 21 by using the "year" key, then you can select those elements with year 21 and mapping then their names:
peaple
.select { |person| person[:year] == "21" }
.map { |person| person[:name] }
# ["Sam", "bart"]