class Array
def map_to_hash
map { |e| yield e }.inject({}) { |carry, e| carry.merge! e }
end
end
使用:
[1, 2, 3].map_to_hash { |e| {e => User.find(e)} }
class Array
def map_to_hash
map { |e| yield e }.inject({}) { |carry, e| carry.merge! e }
end
end
[1, 2, 3].map_to_hash { |e| {e => User.find(e)} }