```
def product_except_self(nums)
left = []
right = []
n = nums.length
(0...nums.length).each do |i|
left[i] = i == 0 ? 1 : left[i - 1] * nums[i - 1]
right[n - 1 - i] = i == 0 ? 1 : right[n - i] * nums[n - i]
end
p = []
(0...nums.length).each do |i|
p[i] = left[i] * right[i]
end
return p
end
```